Python Module 19
Python for coding interviews and online assessments: questions
Questions on python for coding interviews and online assessments: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
Why do coding-round solutions read sys.stdin.buffer instead of calling input() in a loop?
Answered in the lesson Python for coding rounds: input, output, limits
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
Your O(n) Python solution reads 10⁶ numbers with int(input()) in a loop and prints every answer with print(x, flush=True). It exceeds the 2-second limit on the largest test. What do you measure, what do you change, and how do you confirm the fix before you resubmit?
- What it tests
- Separating input and output cost from the algorithm's cost, knowing how Python's standard streams buffer, and proving a fix with a measurement instead of a guess.
- First step
- Build one input of the largest allowed size and time the three phases separately: reading, solving and writing. In a measured run on CPython 3.14.8, input() alone needed about two seconds for a million lines and flushing 200,000 lines over a second, so the algorithm may not be the problem at all.
- Follow-ups
-
- Each line holds a name with spaces in it. How do you read that input fast?
- The problem is interactive. Which of your changes must you undo?
- After the fix the solution still takes 1.8 seconds. What do you look at next?
From the lesson Python for coding rounds: input, output, limits
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A tree-DP solution passes the small tests but fails with RecursionError on a path-shaped tree of 2 × 10⁵ nodes. A teammate adds sys.setrecursionlimit(10**6) and wraps the function in functools.cache. What can still go wrong, and what do you submit instead?
- What it tests
- Knowing what the recursion limit protects, why calls through C code use the C stack, and turning a post-order recursion into a loop with an explicit stack.
- First step
- Separate the two limits: the recursion limit counts Python frames, while the C stack has a fixed size. A cached function passes through C code on every call, and in a measured run on CPython 3.14.8 a cached recursion 10,000 levels deep overflowed the C stack, with the limit at a million. Then rewrite the recursion with an explicit stack.
- Follow-ups
-
- How do you compute subtree sizes iteratively, children before parents?
- When is the thread stack-size workaround acceptable?
- How do you test the fix before you submit?
From the lesson Python for coding rounds: input, output, limits
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Fresher to mid
A text-cleaning job removes 500 stop words from a list of 10⁶ words with while w in words: words.remove(w), and it now runs for many minutes. Explain the cost, fix it without changing the output, and show how you would prove the new cost without trusting a stopwatch.
- What it tests
- Reading the cost of list operations inside a loop, choosing a set, keeping order and repeats, and proving a complexity by counting work.
- First step
- Count the scans: every in and every remove() walks the list from the front, and the while loop repeats both for each copy of a stop word. Stop words are the most frequent words, so there are hundreds of thousands of copies, each costing up to two scans of a million items.
- Follow-ups
-
- Does your fix keep the original order and the repeated words?
- The stop list grows to 10⁵ entries. What changes?
- How would a test catch a return to the quadratic version?
From the lesson Time complexity of Python's built-in operations
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
An API keeps per-request counters in a dict keyed by integer IDs that clients send. One client sends 50,000 IDs and its request takes seconds of CPU. What could the client be doing, why does Python's hash randomisation not stop it, and how do you defend the service?
- What it tests
- Knowing why dict costs are averages, how Python hashes numbers, which types are salted, and choosing defences that hold against hostile input.
- First step
- Measure before guessing: count how many distinct hash values the IDs of one slow request produce, and compare the CPU time per key with a normal request. If most keys share a hash, every insertion compares the new key with all the earlier ones, and the request has become quadratic.
- Follow-ups
-
- Would converting the IDs to strings help, and at what cost?
- How would you notice this attack in production?
- What limits would you put on one request?
From the lesson Time complexity of Python's built-in operations
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
n is 10⁶ and the time limit is 1 second. Your Python solution sorts the input, then loops over it once with a dict lookup and an append per item. Estimate whether it fits, say what you would measure, and what you would change if the estimate is close.
- What it tests
- Turning a complexity into seconds with measured rates, counting reading and writing too, and knowing which levers help when an estimate is tight.
- First step
- Split the time into parts and price each one: reading 10⁶ numbers, the sort, the loop and the output. A measured rate for a simple loop step, tens of millions a second on a fast machine, turns each count of steps into seconds; note which parts run in C.
- Follow-ups
-
- The judge's computer is twice as slow as yours. Does it still fit?
- Which parts of this solution run in C, and why does that matter?
- When would you submit it under PyPy, and what could go wrong?
From the lesson Time complexity of Python's built-in operations
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A batch job needs every prime below 10⁷, and a loop that calls is_prime(n) for each number takes about a minute. Choose a better method, estimate the work and the memory of both, and say what changes if the limit becomes 10⁹.
- What it tests
- Comparing trial division with the sieve, estimating work and memory from counts, and knowing when a segmented sieve is needed.
- First step
- Estimate the current work first: trial division tries up to √n divisors for each number, so covering every number up to 10⁷ costs far more than 10⁷ steps. Then ask whether one pass over a table of 10⁷ flags, crossing out multiples, could replace all those divisions.
- Follow-ups
-
- How much memory does the sieve need at 10⁷, and how do you reduce it?
- Where may the crossing-out for each prime start, and why?
- What do you do at 10⁹?
From the lesson Classic college programs written the Python way
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher
In a lab viva, the examiner points at your prime check and asks why the loop stops at the square root, what it returns for 0, 1 and 2, what it costs, and how you would list every prime below a million. Give the answer you would say aloud.
- What it tests
- Explaining the reason behind an algorithm, knowing its edge cases and its cost, and proposing the right method for a larger version of the problem.
- First step
- Start with the reason, not the code: divisors come in pairs d × e = n, and the smaller of each pair is at most √n, so every composite number has a divisor the loop reaches. Then give the edge cases and point to the tests that check them.
- Follow-ups
-
- Why math.isqrt(n) and not int(n ** 0.5)?
- The loop also tries even divisors. Does that change the complexity?
- What would you test before the viva?
From the lesson Classic college programs written the Python way
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
You must print row 1,000 of Pascal's triangle exactly. Compare building it by repeated addition, calling math.comb for each entry, and the multiplicative formula: their work, memory and exactness, and why a float version is wrong.
- What it tests
- Counting the operations of three methods, knowing that Python ints are exact and floats are not, and choosing the method that fits the question.
- First step
- Count the work of each method before choosing: the additions needed to build every row from row 0, the work each math.comb call repeats, and the steps of a method that goes from one entry of the row to the next. Then check how many digits the middle entry of row 1,000 has.
- Follow-ups
-
- How do you show that every entry stays an exact integer?
- What if you need every row up to 1,000, not just the last?
- The question asks for the row modulo 10⁹ + 7. What changes?
From the lesson Classic college programs written the Python way
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes