ProgrammingHard

For each of Q = 10^5 queries, a program sums an array prefix of length up to N = 10^5 by a loop. What is the total cost?

  1. A. About 10^5 additions, which is fast enough
  2. B. About 10^10 additions, but in a 2-second limit it passes
  3. C. About 5 x 10^9 additions, which is too slow for a 1-second limit
  4. D. About 10^3 additions, since the prefix is short

Correct answer: C. About 5 x 10^9 additions, which is too slow for a 1-second limit

Each query costs up to N additions, so Q x N = 10^10 in the worst case and about 5 x 10^9 on average. Prefix sums reduce each query to O(1).

More C Programming questions

Exams that ask this topic