Katkat

Solve Four Interview Coding Problems

PinterestSoftware Engineer · Onsite

Last updated

This set tests proficiency in graph algorithms, binary search on sorted data, grid simulations, and custom data structure design for real-time leaderboards.

Part 1: Itinerary Reconstruction

Given a list of flight tickets represented as [from, to] pairs, reconstruct an itinerary that uses every ticket exactly once. If multiple valid itineraries exist, return the one that is lexicographically smallest. If no such itinerary exists, return an empty list.

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

Design a class to store values associated with timestamps. The class must support put(key, value, timestamp) and get(key, timestamp), where get returns the value for a key at or immediately before the given timestamp.

class TimeMap: ...

Part 3: Ray Grid Traversal

Given an N x M grid where each cell is either 0 (empty) or 1 (block), a ray starts at (r, c) moving in direction (dr, dc). When it hits a 1, it reflects 90 degrees clockwise. Return the path of the ray as a list of coordinates until it enters an infinite loop or exits the grid.

Part 4: High-Performance Leaderboard

Design a system to track scores for players. Support:

  • update_score(player_id, score): Update a player's score.
  • top_k(k): Return the IDs of the top K players.
  • Assume N=1,000,000 players and high write frequency.

Examples

Part 2: tm.put('foo', 'bar', 1) tm.get('foo', 3) -> returns 'bar' tm.get('foo', 0) -> returns None

Constraints

  • Part 1: Tickets length up to 10,000.
  • Part 2: Timestamps are strictly increasing for each key.
  • Part 4: top_k must be optimized for sub-linear time complexity.

Hints

Hint 1

For Part 1, use Hierholzer’s algorithm or backtracking with a greedy approach to find the lexicographical path.

Hint 2

For Part 2, store values for each key in a list and use binary search (`bisect_right`) to find the latest valid timestamp.

Hint 3

For Part 4, use a combination of a Hash Map to track scores and a Sorted List or Balanced BST to maintain the leaderboard.

Follow-up questions

  • How would you modify the TimeMap if timestamps were not strictly increasing?
  • In the Leaderboard problem, how would you handle ties in scores?
  • For the ray traversal, what is the maximum number of steps the ray can take before you can guarantee it is in a loop?