Skip to content
Preptima
mediumCodingConceptMidSenior

The assistant wrote a working solution and the interviewer asks for its complexity and one input that breaks it. How do you verify code you did not write?

Read every loop and recursive call, then price each line inside them, because the expensive part is often a library call such as pop(0), a list membership test or string concatenation. Aim inputs at that worst case and at the edges the samples skip, and compare against a slow, simple version on random inputs.

5 min readUpdated 2026-10-10

Answer

Short answer

Read it as if a stranger wrote it, because one did. Complexity analysis of generated code means finding every loop and recursive call, then pricing each line inside them, since the cost that matters is often a library call that looks free. Then build inputs that aim at what you found: the worst case for the hidden cost, and the edges the sample tests never exercised. Finally, compare the code against a slow, obviously correct version on thousands of small random inputs.

Was this answer helpful?

Likely follow-ups

  • Your randomised comparison passes ten thousand cases. What kind of bug can it still be hiding?
  • The interviewer says inputs never exceed a thousand intervals. Does the pop(0) finding still matter?
  • How would you find a hidden quadratic cost if you could run the code but not read it?
  • The solution is recursive and correct. Which input breaks it in Python that would not break an iterative version?

Related questions

Further reading