PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question set evaluates algorithmic problem-solving skills including graph traversal and itinerary reconstruction, time-sorted search and binary search, grid-based simulation of ray traversal, and data structure design for efficient updates and leaderboard generation.

  • medium
  • Pinterest
  • Coding & Algorithms
  • Software Engineer

Solve Four Interview Coding Problems

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

The interview included four coding problems of the following form: 1. Given a list of airline tickets represented as `[from, to]`, use every ticket exactly once to build a valid itinerary that starts from a fixed airport such as `JFK`. If multiple valid itineraries exist, return the lexicographically smallest one. 2. Given a list of log records, each with a date-string timestamp, sort the logs by time and then use binary search to return the index of a target timestamp, or the insertion position if the target does not exist. 3. Given a 2D grid representing a lighthouse puzzle, simulate how a light ray travels through the grid. The ray may pass through empty cells, stop at blockers, and change direction at reflection cells. Return the illuminated path or the final stopping state. 4. Design a data structure for an escape-room style game in which players move only forward through rooms. Support player advancement in `O(1)`, querying a player's current room in `O(1)`, and generating a leaderboard in `O(N + k)`, ordered by farthest position reached, with ties broken by the order in which players entered their current room.

Quick Answer: This question set evaluates algorithmic problem-solving skills including graph traversal and itinerary reconstruction, time-sorted search and binary search, grid-based simulation of ray traversal, and data structure design for efficient updates and leaderboard generation.

Part 1: Reconstruct Itinerary from Airline Tickets

Given a list of airline tickets where each ticket is [from, to], build an itinerary that starts at the given airport and uses every ticket exactly once. If more than one valid itinerary exists, return the lexicographically smallest complete itinerary.

Constraints

  • 0 <= len(tickets) <= 10^4
  • Each ticket is a pair [from, to] of non-empty strings
  • Duplicate tickets may exist
  • Assume at least one valid itinerary exists for the given start, except the empty-ticket case

Examples

Input: ([['MUC', 'LHR'], ['JFK', 'MUC'], ['SFO', 'SJC'], ['LHR', 'SFO']], 'JFK')

Expected Output: ['JFK', 'MUC', 'LHR', 'SFO', 'SJC']

Explanation: There is only one valid way to use all tickets starting from JFK.

Input: ([['JFK', 'SFO'], ['JFK', 'ATL'], ['SFO', 'ATL'], ['ATL', 'JFK'], ['ATL', 'SFO']], 'JFK')

Expected Output: ['JFK', 'ATL', 'JFK', 'SFO', 'ATL', 'SFO']

Explanation: Multiple itineraries exist, so the lexicographically smallest valid one must be chosen.

Input: ([], 'JFK')

Expected Output: ['JFK']

Explanation: Edge case: with no tickets, the itinerary is just the starting airport.

Input: ([['JFK', 'A'], ['JFK', 'A'], ['A', 'JFK']], 'JFK')

Expected Output: ['JFK', 'A', 'JFK', 'A']

Explanation: Duplicate tickets must still be treated as separate edges and used exactly once.

Hints

  1. Using every ticket exactly once is a strong hint toward an Eulerian path/trail approach.
  2. If you sort destinations in reverse order, you can pop the smallest next destination efficiently from the end.

Part 2: Sort Timestamped Logs and Find Target Position

You are given log records where each record is [timestamp, message]. Timestamps use the zero-padded format YYYY-MM-DD HH:MM:SS, so normal string comparison matches chronological order. Sort the logs by timestamp, then use binary search to return the leftmost index of the target timestamp, or the insertion position if the target does not exist.

Constraints

  • 0 <= len(logs) <= 10^5
  • Each log is [timestamp, message]
  • Timestamps are valid zero-padded strings in the format YYYY-MM-DD HH:MM:SS
  • If multiple logs share the same timestamp, return the leftmost matching index after sorting

Examples

Input: ([['2024-03-01 09:00:00', 'A'], ['2024-02-28 12:00:00', 'B'], ['2024-03-01 08:00:00', 'C']], '2024-03-01 08:00:00')

Expected Output: ([['2024-02-28 12:00:00', 'B'], ['2024-03-01 08:00:00', 'C'], ['2024-03-01 09:00:00', 'A']], 1)

Explanation: After sorting, the target timestamp appears at index 1.

Input: ([['2024-03-01 09:00:00', 'A'], ['2024-02-28 12:00:00', 'B'], ['2024-03-01 08:00:00', 'C']], '2024-03-01 08:30:00')

Expected Output: ([['2024-02-28 12:00:00', 'B'], ['2024-03-01 08:00:00', 'C'], ['2024-03-01 09:00:00', 'A']], 2)

Explanation: The target is missing, so return the insertion position between 08:00 and 09:00.

Input: ([['2024-01-01 00:00:00', 'x'], ['2024-01-01 00:00:00', 'y'], ['2023-12-31 23:59:59', 'z']], '2024-01-01 00:00:00')

Expected Output: ([['2023-12-31 23:59:59', 'z'], ['2024-01-01 00:00:00', 'x'], ['2024-01-01 00:00:00', 'y']], 1)

Explanation: For duplicate timestamps, return the leftmost matching index.

Input: ([], '2024-01-01 00:00:00')

Expected Output: ([], 0)

Explanation: Edge case: an empty log list has insertion index 0.

Hints

  1. Because the timestamps are zero-padded, you do not need to parse them into datetime objects to compare them correctly.
  2. Use a lower-bound style binary search: find the first position whose timestamp is not less than the target.

Part 3: Simulate a Lighthouse Light Ray in a Grid

You are given a rectangular grid representing a lighthouse puzzle. A ray starts inside the grid at the given cell and direction. The starting cell is illuminated immediately. Empty cells let the ray continue straight, blocker cells stop the ray before it enters them, slash mirrors reflect the ray, and backslash mirrors reflect the ray the other way. If the same (row, col, direction) state repeats, report a loop.

Constraints

  • 1 <= number of rows, number of columns <= 200
  • All rows have the same length
  • Grid cells contain only '.', '#', '/', or backslash
  • The start position is inside the grid and is not a blocker

Examples

Input: (['...', '...'], (0, 0), 'R')

Expected Output: ([(0, 0), (0, 1), (0, 2)], 'EXIT', (0, 3), 'R')

Explanation: The ray moves straight across the first row and exits the grid.

Input: (['..#', '...'], (0, 0), 'R')

Expected Output: ([(0, 0), (0, 1)], 'BLOCKED', (0, 2), 'R')

Explanation: The blocker is not included in the illuminated path.

Input: (['.\\.', '...', '...'], (0, 0), 'R')

Expected Output: ([(0, 0), (0, 1), (1, 1), (2, 1)], 'EXIT', (3, 1), 'D')

Explanation: The backslash mirror turns the ray downward.

Input: (['/\\', '\\/'], (0, 0), 'U')

Expected Output: ([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)], 'LOOP', (0, 0), 'U')

Explanation: The mirrors create a cycle that returns to the same cell with the same direction.

Input: (['.'], (0, 0), 'L')

Expected Output: ([(0, 0)], 'EXIT', (0, -1), 'L')

Explanation: Edge case: a single-cell grid exits immediately after the start cell.

Hints

  1. Track full states as (row, col, direction); revisiting the same cell with a different direction is not the same state.
  2. Apply the mirror effect of the current cell before moving to the next cell.

Part 4: Escape-Room Leaderboard Data Structure

Players move only forward through rooms numbered 0 to num_rooms - 1. Process a list of operations while supporting O(1) enter, O(1) one-room advancement, O(1) current-room queries, and leaderboard generation in O(num_rooms + k). The leaderboard should list the farthest players first, and players in the same room should be ordered by when they entered that room.

Constraints

  • 1 <= num_rooms <= 10^5
  • 0 <= len(operations) <= 2 * 10^5
  • A player enters at most once; duplicate enters may be ignored
  • Advancing a player already in the last room is a no-op
  • Querying a missing player with 'where' should return -1

Examples

Input: (4, [('enter', 'alice'), ('enter', 'bob'), ('advance', 'alice'), ('where', 'alice'), ('leaderboard', 2)])

Expected Output: [1, ['alice', 'bob']]

Explanation: Alice is farther ahead, so she leads the leaderboard.

Input: (5, [('enter', 'bob'), ('enter', 'alice'), ('advance', 'alice'), ('advance', 'bob'), ('leaderboard', 2)])

Expected Output: [['alice', 'bob']]

Explanation: Both players are in room 1, but Alice entered room 1 earlier, so she comes first.

Input: (3, [('enter', 'a'), ('enter', 'b'), ('enter', 'c'), ('advance', 'a'), ('advance', 'a'), ('advance', 'b'), ('leaderboard', 5), ('where', 'c')])

Expected Output: [['a', 'b', 'c'], 0]

Explanation: The leaderboard is ordered by room 2, then room 1, then room 0.

Input: (2, [('leaderboard', 3), ('where', 'ghost')])

Expected Output: [[], -1]

Explanation: Edge case: no players have entered yet.

Input: (1, [('enter', 'solo'), ('advance', 'solo'), ('where', 'solo'), ('leaderboard', 1)])

Expected Output: [0, ['solo']]

Explanation: With only one room, advancing changes nothing.

Hints

  1. Keep players bucketed by room, and preserve arrival order inside each room.
  2. A hash map from player ID to its current node/room lets you remove and reinsert a player in O(1) when they advance.
Last updated: Apr 21, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Hierarchical Access Control for an Advertising Platform - Pinterest (medium)
  • First Word Matching Each Prefix Query - Pinterest (medium)
  • Maximize Boxes Stored Through One Entrance - Pinterest (medium)
  • Solve Multiple Coding Interview Problems - Pinterest (medium)
  • Implement a Sparse Matrix Class - Pinterest (medium)