Mock online coding assessment
75 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)An online coding assessment is the first filter in many placement drives: two problems, one timer, and hidden tests that reward only programs that are correct, fast and print exactly the expected output. Set a 60-minute timer and try both problems before you read any solution.
Mock rules: two problems, 60 minutes, Python 3, stdin to stdout
An online coding assessment (OA) is a timed test where you solve a few problems in an online editor, your program reads input from standard input, and hidden test cases decide your score.
Standard input (stdin) is where the platform sends the test input; standard output (stdout) is what it reads back, character for character. sys.stdin.read().split() reads every token at once, whatever the line layout, and is much faster than calling input() many times.
import sys
data = sys.stdin.read().split()n = int(data[0])numbers = list(map(int, data[1:1 + n]))Rules for this mock: 60 minutes for both problems, no internet search, and print exactly what the output format says (no extra text such as "Answer:"). Platforms differ in Python version and time limits, so check the real instructions.
Problem 1: the longest run within a cloud budget
A sliding window is a range [left, right] over an array that grows on the right and shrinks on the left, so every element enters and leaves the window at most once.
Problem 1 asks for the longest run of days whose total cost stays within a budget. Try it for 20 minutes first, then compare with the model solution.
Example 1 · Problem 1 statement
Problem 1: the longest run within a budgetA startup records its cloud spend for n days in a row: spend[1..n] dollars.Find the longest run of consecutive days whose total is at most B dollars.Print its length, or 0 if no single day fits.Input: line 1 has n and B; line 2 has n numbersLimits: 1 <= n <= 2 x 10^5, 1 <= spend[i] <= 10^9, 0 <= B <= 10^14Sample input:7 500120 80 300 50 60 90 4004
What this code does
- Read the limits first: n = 2 × 10⁵ rules out O(n²), and a total of up to 2 × 10¹⁴ does not fit in
int. - In the sample, days 2 to 5 cost 80 + 300 + 50 + 60 = 490, which is at most 500, and no run of five days fits.
- Try the problem yourself before you read the model solution below.
Example 2 · Problem 1 model solution (sliding window)
import sys
def main(): data = sys.stdin.read().split() n, budget = int(data[0]), int(data[1]) spend = list(map(int, data[2:2 + n]))
total = 0 # cost of the current window spend[left..right] left = 0 best = 0 for right, cost in enumerate(spend): total += cost while total > budget: # too costly: drop days from the left total -= spend[left] left += 1 best = max(best, right - left + 1) print(best)
main()4
What this code does
- The window
spend[left..right]always has a total of at most B after thewhileloop, so its length is a valid answer. - Each index moves forward only, so the two loops together do at most 2n steps: O(n).
sys.stdin.read().split()reads every number at once, whatever the line layout. Pythonintnever overflows, so a total of 2 × 10¹⁴ is safe.- Sample input:
7 500 / 120 80 300 50 60 90 400.
The model solution adds the new day on the right and, while the total is above B, drops days from the left. Time is O(n), because each day enters and leaves the window once; extra space is O(n) for the list.
Python int has no upper limit, so a total of 2 × 10¹⁴ needs no special type. What Python does punish is slow loops: about 10⁷ simple steps a second, so O(n) matters. The window works only because every spend is positive. Revise: Variable-size window (opens in a new tab). More practice: the Python DSA 150 sheet (open it at /learn/python/dsa-sheet) and the sliding window must-do problems (opens in a new tab).
Problem 2: the minimum number of classrooms
The minimum rooms problem asks for the fewest rooms that fit all lectures. The answer is the largest number of lectures running at the same moment.
Problem 2 is the classic "Minimum Platforms" problem. A room frees up when its lecture ends, so back-to-back lectures can share one room.
Example 3 · Problem 2 statement
Problem 2: the minimum number of classroomsA college timetable has n lectures, each with a start and an end time.A room is free for a new lecture once the previous lecture's end time isreached: one ending at 10 and one starting at 10 can share a room.Print the minimum number of rooms needed.Input: line 1 has n; then n lines of "start end"Limits: 1 <= n <= 10^5, 0 <= start < end <= 10^9Sample input:69 109 129 1110 1111 1312 133
What this code does
- The answer is the largest number of lectures running at the same moment.
- In the sample, three lectures start at 9 and overlap, so 3 rooms are needed; later lectures reuse rooms that were freed.
- The tie rule in the statement (end equals start can share) decides several hidden tests.
Example 4 · Problem 2 model solution (sorted starts and ends)
import sys
def main(): data = sys.stdin.read().split() n = int(data[0]) start = [int(x) for x in data[1:2 * n + 1:2]] end = [int(x) for x in data[2:2 * n + 2:2]] start.sort() end.sort()
rooms = best = j = 0 # j: next lecture to finish for begin in start: # lectures in order of start time while j < n and end[j] <= begin: rooms -= 1 # that lecture is over: its room is free j += 1 rooms += 1 best = max(best, rooms) print(best)
main()3
What this code does
- Sorting starts and ends separately works because only the count of running lectures matters, not which lecture is which.
end[j] <= beginfrees a room for a lecture starting exactly when another ends; using<here fails the tie tests.- Sorting costs O(n log n); the walk is O(n), since
jonly moves forward. - Sample input:
6 / 9 10 / 9 12 / 9 11 / 10 11 / 11 13 / 12 13.
The model solution sorts the start times and the end times separately, then walks through the starts in order. Before each start it frees every room whose lecture has ended (end ≤ start), then takes a room. Time is O(n log n) for sorting and O(n) for the walk; extra space is O(n).
The tie rule (end ≤ start frees the room) is the most common hidden-test trap. A heapq of end times solves it too. Revise: Meeting rooms (opens in a new tab) and the greedy and intervals must-do problems (opens in a new tab).
Sample and hidden-style tests, and what partial scores tell you
Hidden tests are the many tests a platform runs after you submit, usually including edge cases and the largest inputs, so passing the sample tests proves very little.
Read your verdict like a report: TLE on the largest tests points to a slow algorithm or slow input reading, WA on large tests to a wrong approach or / float division on huge numbers, WA on small tests to a logic or edge-case bug.
Example 5 · Problem 1 brute force: correct but too slow
import sys
def main(): data = sys.stdin.read().split() n, budget = int(data[0]), int(data[1]) spend = list(map(int, data[2:2 + n]))
best = 0 for i in range(n): # every start day total = 0 for j in range(i, n): # every end day: O(n^2) in total total += spend[j] if total > budget: break best = max(best, j - i + 1) print(best)
main()4
What this code does
- It tries every start day and extends to the right until the total passes B: correct, and a good first submission if you are stuck.
- For n = 2 × 10⁵ it can take about 2 × 10¹⁰ steps, and Python runs roughly 10⁷ simple steps a second, so the largest hidden tests show TLE.
- A partial score such as "6 of 10 tests passed, the rest TLE" is exactly this pattern.
- Sample input:
7 500 / 120 80 300 50 60 90 400.
Example 6 · A stress test: brute force versus sliding window
import random
def window(spend, budget): total = left = best = 0 for right, cost in enumerate(spend): total += cost while total > budget: total -= spend[left] left += 1 best = max(best, right - left + 1) return best
def brute(spend, budget): best = 0 for i in range(len(spend)): total = 0 for j in range(i, len(spend)): total += spend[j] if total > budget: break best = max(best, j - i + 1) return best
rng = random.Random(42) # fixed seed: the same tests every runtests = 300for t in range(1, tests + 1): spend = [rng.randint(1, 20) for _ in range(rng.randint(1, 8))] budget = rng.randint(0, 59) if window(spend, budget) != brute(spend, budget): print("Mismatch on test", t) breakelse: print("Sample answer:", window([120, 80, 300, 50, 60, 90, 400], 500)) print(tests, "random tests: all match")Sample answer: 4 300 random tests: all match
What this code does
- Small random inputs (n up to 8, values up to 20) cover edge cases you might not think of, such as B = 0 or every day too costly.
- If the two methods ever disagree, the program prints the test number, and you can print that input to debug it. The
elseof aforloop runs only when the loop was not stopped bybreak. - A fixed seed (
random.Random(42)) makes the run repeatable; remove the seed to try new tests each time.
Sample tests are the examples in the problem. A partial score counts the hidden tests you passed, often with a verdict such as Wrong Answer (WA) or Time Limit Exceeded (TLE). A stress test compares your fast solution with a simple brute force on many small random inputs and finds such bugs before you submit.
Edge cases to try:
- Problem 1: n = 1; B = 0; one day costs more than B; all days fit; values near 10⁹.
- Problem 2: n = 1; all lectures at the same time; back-to-back lectures; no overlaps.
- Largest tests: n at the maximum, to catch O(n²) code and recursion limits.
Coding-round checklist
A coding-round checklist is the short list you run through before every submit, to catch the mistakes that cost the most marks: extra output, wrong format, slow input and missed edge cases.
Run this list for about one minute per problem, just before you press submit.
[ ] Input and output format exactly as stated[ ] No debug prints and no prompts like "Enter n"[ ] Read with sys.stdin, not input() in a loop of 10^5 or more[ ] // for integer division; / gives a float and loses digits above 2^53[ ] Complexity fits the limits (n = 2 x 10^5 needs O(n) or O(n log n))[ ] Tried n = 1, all equal values, the "no answer" case, ties[ ] Ran the sample input once more after the last changeRevise: Coding round time strategy (opens in a new tab) and Online coding round format (opens in a new tab).
For more coding practice use the Python DSA 150 sheet (open it at /learn/python/dsa-sheet). Practise the spoken side of the same round in the timed Mock Interview Mode (opens in a new tab).
Next step
Check your understanding
12 quick questions