The Python round
Part of the free LogicWiz Data Engineering course, Module 18: Interview rounds. The SQL and Python run in the app, on real Postgres and real Python in the browser.
It is the Python round. The interviewer pastes three lines of SwiftKart's event log into the editor and says: "No pandas, please. Turn this into one row per item, drop the repeats and count the sessions." The task is not hard. They are watching two things: which data structure you reach for, and whether your code survives the record that is missing a field.
You'll learn to
- Solve the classic plain-Python data tasks: flatten, dedup, sessionise, join, top-k
- Pick the right structure for each task, and say what it costs
- Write small functions that a test can call on new data
Learn
How the round runs
You usually get 45 minutes and a plain editor: CoderPad, a shared document or a notebook. And no pandas. The interviewer wants to see you handle records with dicts, lists, sets and the standard library.
Consulting and IT services firms tend to ask practical file and JSON tasks. Product companies often add a twist on scale: "what if the file is 50 GB?"
You are graded on three things. Is it correct on awkward records? Is it readable: small functions, clear names? Can you say what it costs in time and memory?
So write a function, not a script, and say what it returns before you write it. Every problem below is a function that hidden tests call on new data.
The toolbox
| Task | Reach for | Cost |
|---|---|---|
| Join two lists of records | build a dict from one list, look up the other | O(n + m), not O(n x m) |
| Dedup, keep the latest | dict keyed by a tuple of the key fields | O(n) time, one entry per key |
| Group and count or sum | dict.get(k, 0), defaultdict, Counter | O(n) |
| A file bigger than memory | a generator that yields one row at a time | O(1) memory |
| Top k of n | heapq.nlargest(k, ...) | O(n log k) |
| Sessions, overlapping ranges | sort once, then one sweep comparing with the previous item | O(n log n) |
| Nested JSON | a recursive function over dict.items() | O(size of the record) |
| Messy text lines | str.split(), str.partition(), validate every field | count the bad lines, do not crash |
Find your task in the left column and read across. Then say the cost out loud: "I build a dict of customers first, so the join is linear" is exactly the sentence interviewers wait for.
Build one answer: keep the latest version
Here is the first row of the toolbox at work. SwiftKart's landing folder holds 68 order rows. An order appears again each time its status changes, and one file was sent twice. The question: what is each order's latest status?
The answer is one dict, keyed by order_id. Read the rows one at a time. For each row, look up its order in the dict. If the order is not there yet, or this row is newer than the one you kept, store this row. Otherwise ignore it.
Follow order 1013 through three files.
What the dict holds for order 1013
Three rows for order 1013 arrive from three files: the first is stored, the copy from the retry file is ignored because it is not newer, and the shipped row from 4 June replaces the stored one.
First time seen. The 3 June file brings order 1013 as placed. The dict has no entry for 1013, so the row goes in.
The retry file. The same row again, with the same
updated_at. It is not newer than the entry, so it is ignored.A real change. The 4 June file says order 1013 shipped, with a later
updated_at. Newer wins: the entry is replaced.
The left box is the row being read; the right box is the dict after it. Whatever arrives, the dict keeps one entry per order: 68 rows in, 40 orders out.
The same thing as code: one pass, one dict
import csv, glob
latest = {}
rows_read = 0
for path in sorted(glob.glob('landing/orders/*.csv')):
with open(path, newline='') as f:
for row in csv.DictReader(f):
rows_read += 1
kept = latest.get(row['order_id'])
if kept is None or row['updated_at'] > kept['updated_at']:
latest[row['order_id']] = row
print('rows read:', rows_read)
print('orders kept:', len(latest))
print('order 1013 is now:', latest['1013']['status'])
The if line is the whole idea: store the row when the order is new or the row is newer. The ISO timestamps all have the same format, so comparing them as strings orders them correctly. With mixed formats, parse them with datetime.fromisoformat first.
Sort, then sweep: counting sessions
from datetime import datetime
stamps = ['2025-06-01T16:37:56Z', '2025-06-01T15:21:59Z',
'2025-06-01T15:34:59Z', '2025-06-01T17:02:56Z']
times = sorted(datetime.fromisoformat(s) for s in stamps)
sessions = 1
for prev, cur in zip(times, times[1:]):
if (cur - prev).total_seconds() > 30 * 60:
sessions += 1
print('sessions:', sessions)
The other shape worth knowing by heart. Sessions, merged time ranges and streaks all work this way: sort once, then walk the list comparing each item with the one before it.
"What if the file is 50 GB?": two ways to read it
Two paths from a 50 GB CSV file: readlines builds a list of every line in memory and runs out of memory, while a generator hands one row at a time to a dict of running totals that holds one entry per order.
Follow the red path, then the green one. The red path holds every line at once: on a 64-bit server each Python str carries a 41-byte header before its text (sys.getsizeof('') in CPython 3.12; the browser's 32-bit build shows 21), and the list adds an 8-byte pointer per line, so the list is bigger than the file. The green path holds one entry per key. That is why the answer is "stream it, and keep only the totals".
Strong candidates talk while they type: "I will key a dict by (order_id, line_no); on equal timestamps the later row wins, because it arrived later." When you finish, test with one awkward record (a missing key, a tie, an empty list) and state the complexity. If asked about scale, say what changes: stream with a generator, or move the job to Spark.
The trap: bugs interviewers plant on purpose. CSV values are strings, so max of prices compares text, and '9.99' beats '89.00'. A record without a field crashes row['items']: use row.get('items') or []. Timestamps in two formats sort wrongly as strings. And f.readlines() on a 50 GB file loads all of it: iterate over the file instead.
Remember: a dict to find things, a sort to compare neighbours, a generator when the file is big. Then say the cost.
Next is the PySpark round: the same problems again, written as DataFrame code.
In one line
Plain-Python data tasks come down to a dict to find things, a sort to compare neighbours and a generator for big files, plus a check on every messy field.
Interview corner
How would you process a file that does not fit in memory, in plain Python?
Stream it: iterate over the file object (or csv.DictReader) inside a generator, so only one line is in memory at a time, and keep just the aggregates, such as a dict of running totals. If even the aggregates are too big, process the file in chunks by key, or move to a tool built for it (DuckDB, Spark).
How do you join two lists of records efficiently?
Build a dict from the smaller list keyed by the join key, then make one pass over the other list and look each key up. That is O(n + m) instead of the O(n x m) of a nested loop; it is exactly a hash join. dict.get gives left-join behaviour, and a dict of lists handles duplicate keys.
How do you deduplicate records and keep the latest version?
Key a dict by the record's key (a tuple for a composite key) and replace the stored row only when the new one has a later timestamp. I agree a tie rule up front, for example "the later arrival wins", and say what happens to rows with a missing timestamp.