ProgrammingHardCodeforces-style · placement coding round

With n = 2 x 10^5 and a 1-second limit in CPython, which algorithm can be rejected because it is too slow?

  1. A. Sort the array once and use two pointers, about 3.5 x 10^6 steps
  2. B. A nested loop over all pairs, about 2 x 10^10 checks
  3. C. A dictionary counting pass, about 2 x 10^5 steps
  4. D. A binary search for each element, about 3.5 x 10^6 steps

Correct answer: B. A nested loop over all pairs, about 2 x 10^10 checks

CPython manages roughly 10^7 simple operations per second, so n^2 pair checks with n = 2 x 10^5 is far too slow. The other three are O(n) or O(n log n).

More Python Programming questions

Exams that ask this topic