KatInterview

Four Algorithm Problems: Graphs, Search, Simulation, and Design

Data Structure DesignModel answer
MediumModel answerData Structure Designgraphsbinary-searchsimulation

Last updated

Solve four independent problems covering Eulerian path reconstruction, timestamped key-value lookup, ray reflection simulation, and a high-throughput score tracker.

Overview

Four self-contained problems. Each tests a different skill. Solve them in order — they increase in design complexity. For problems with ambiguous behavior, state your assumptions before writing code.


Problem 1: Reconstruct a Flight Route

You are given a list of one-way flight tickets, each represented as [origin, destination]. Reconstruct a single route that uses every ticket exactly once. If more than one valid route exists, return the lexicographically smallest one. If no valid route exists (the tickets cannot form a single continuous path), return an empty list.

def find_itinerary(tickets: list[list[str]]) -> list[str]

Example:

tickets = [["LAX","ORD"],["JFK","LAX"],["ORD","JFK"]]
→ ["JFK","LAX","ORD","JFK"]

Constraints:

  • Up to 10,000 tickets.
  • Airport codes are uppercase strings.
  • Duplicate tickets are allowed and represent distinct flights.

Problem 2: Versioned Key-Value Store

Build a class that stores values tagged with integer timestamps. It must support two operations:

  • put(key, value, timestamp) — store the value for the given key at the given timestamp.
  • get(key, timestamp) — return the value stored for that key at the largest timestamp less than or equal to the given one. Return None if no such entry exists.
class TimeMap:
    def put(self, key: str, value, timestamp: int) -> None: ...
    def get(self, key: str, timestamp: int): ...

Example:

tm = TimeMap()
tm.put("city", "Paris", 10)
tm.get("city", 15)  →  "Paris"
tm.get("city", 5)   →  None

Constraints:

  • For any given key, put calls arrive in strictly increasing timestamp order.
  • Optimize get for O(log n) time.

Problem 3: Bouncing Ray on a Grid

You have an N×M grid where each cell is either 0 (open) or 1 (blocked). A ray starts at cell (r, c) traveling in direction (dr, dc). At each step:

  • Look at the next cell in the current direction.
  • If it is outside the grid, the ray exits — stop.
  • If it is blocked (1), rotate the direction 90° clockwise without moving.
  • If it is open (0), move there.

Return the list of coordinates the ray visits (including the start), stopping when the ray exits the grid or enters an infinite loop.

def ray_path(grid: list[list[int]], r: int, c: int, dr: int, dc: int) -> list[tuple[int,int]]

Constraints:

  • Grid dimensions up to 1,000×1,000.
  • The starting cell is always open.
  • Directions use row/column convention: up is (-1,0), right is (0,1).

Problem 4: Real-Time Score Leaderboard

Design a leaderboard for a game with up to 1,000,000 players and a very high rate of score updates.

Support these operations:

  • update_score(player_id, score) — set the player's score to the new value (replacing any previous score).
  • top_k(k) — return the IDs of the top k players by score, highest first.

top_k must run in sub-linear time relative to the total number of players. Explain your data structure choice and the complexity of each operation.

Hints

Hint 1

For Problem 1, think of each ticket as a directed edge and the whole problem as finding an Eulerian path. The tricky part is choosing the start node correctly when no origin is explicitly given — derive it from in/out degree counts.

Hint 2

For Problem 2, since timestamps are strictly increasing per key, the stored list is always sorted. That means you can use binary search directly — no sorting needed at query time.

Hint 3

For Problem 4, a plain heap won't work because it can't efficiently update or delete an arbitrary entry. Think about what data structure supports O(log n) insert, delete, and ordered iteration — that's your ordered index.