Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The array [10, 20, 30, 40, 50] is searched for the first index i whose value is strictly greater than the average of all elements before it. For i = 0 there is no earlier element, so index 0 never qualifies. What is that index (report −1 if none exists)?

  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 3

Correct answer: A. 1

Keep a running sum and compare each value against the average of the prefix before it. Comparing against the overall average instead answers a different question.

More Coding Interview Problems questions

Exams that ask this topic