You are working in an AI-assisted coding interview. You may consult AI-generated suggestions, but you must validate them, explain your reasoning, and share your screen while you work.
You are given a small in-memory database abstraction. A table is a list of rows, where each row is a dictionary from column name to value. Queries return a cursor object that supports incremental iteration.
A simplified implementation is shown below:
class Cursor:
def __init__(self, rows):
self.rows = rows
self.index = 0
def has_next(self):
self.index += 1
return self.index < len(self.rows)
def next(self):
if not self.has_next():
return None
return self.rows[self.index]
class Table:
def __init__(self):
self.rows = []
def insert(self, row):
self.rows.append(row)
def query(self, predicate=lambda row: True):
return Cursor([row for row in self.rows if predicate(row)])
Answer the following four parts:
-
Bug finding:
Identify the cursor iteration bug or bugs, explain the expected cursor semantics, and fix the implementation.
-
New feature design:
Add support for one of the following features and describe the design:
limit(n)
,
order_by(column)
, or pagination with a stable resume token.
-
Pull request review:
Review an AI-generated change that eagerly copies all rows on every query and stores mutable row references in the cursor. What correctness, performance, and maintainability concerns would you raise?
-
Production debugging:
Suppose users report that some paginated queries return duplicate rows or skip rows while writes are happening concurrently. Explain how you would investigate, reproduce, instrument, and fix the issue.