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)?
- A. 1
- B. 2
- C. 0
- 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
- Problem: Bob has the numbers 1 to 10 written on a board. He repeatedly erases the two largest numbers and writes their difference. After 9…
- Problem: The string "racecar" is compressed by storing each character and the number of distinct palindromic substrings of length at least…
- Problem: What is the sum of all integers below 1000 that are multiples of 3 or 5? Run the code. python print(sum(x for x in range(1000) if…
- Problem: How many divisors does 10^6 have? Run the code. python print(sum(1 for d in range(1, 10 6 + 1) if (10 6) % d == 0)) What is…
- Problem: What is the largest prime below 1000? Run the code. python def isp(n): return n > 1 and all(n % d for d in range(2, int(n 0.5) +…
- Problem: How many paths go from (0,0) to (4,4) on a 5 x 5 grid using right and down moves while avoiding cell (2,2)? Run the DP. python n…