Magicsheet logo

Average Value of Even Numbers That Are Divisible by Three

Easy
38.7%
Updated 6/1/2025

Asked by 2 Companies

Topics

Average Value of Even Numbers That Are Divisible by Three

What is this problem about?

The Average Value of Even Numbers That Are Divisible by Three interview question is a simple filtering and averaging task. Given an array of integers, you must find the average of all elements that are both even AND divisible by three. If no such numbers exist, the average is 0. This Average Value of Even Numbers That Are Divisible by Three coding problem tests basic loop and condition logic.

Why is this asked in interviews?

Accenture and IBM use this as an entry-level screening question. It ensures a candidate can correctly implement multiple conditions (logical AND) and handle edge cases like empty filtered lists (to avoid division by zero).

Algorithmic pattern used

This utilizes the Array, Math interview pattern. The key logic is identifying that a number is both even and divisible by 3 if it is a multiple of 6 (n % 6 == 0). You iterate through the array once, maintain a sum and a count, and perform the division at the end.

Example explanation

Array: [1, 3, 6, 10, 12, 15]

  1. Filter:
    • 1: No.
    • 3: No (not even).
    • 6: Yes (even and div by 3).
    • 10: No (not div by 3).
    • 12: Yes (even and div by 3).
    • 15: No (not even).
  2. Sum: 6 + 12 = 18. Count: 2.
  3. Result: 18 / 2 = 9.

Common mistakes candidates make

  • Missing One Condition: Only checking if a number is divisible by 3 but forgetting to check if it's even.
  • Division by Zero: Failing to check if any numbers matched the criteria before dividing, which causes a crash.
  • Integer vs Float: Using integer division when a precise floating-point average might be expected (though usually, these problems ask for the floor or an integer result).

Interview preparation tip

Always combine multiple conditions into the most efficient form. Checking n % 6 == 0 is faster and cleaner than checking n % 2 == 0 && n % 3 == 0.

Similar Questions