PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers
|Home/Coding & Algorithms/Pinterest

Support room moves and query top-k fastest

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of dynamic data structure design and ordering semantics, specifically maintaining per-room FIFO arrival order, constant-time room counts, and efficient top-k retrieval based on hierarchical priorities.

  • hard
  • Pinterest
  • Coding & Algorithms
  • Machine Learning Engineer

Support room moves and query top-k fastest

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

## Problem There are `R` rooms labeled `0..R-1` (in increasing order), and `P` people labeled `0..P-1`. - Initially, **all people are in room 0**. - Operation `move(personId)` moves that person **from room i to room i+1**, if `i < R-1`. (If already in the last room, they stay there.) - Within each room, maintain the **arrival order** of people (FIFO by the time they entered that room). Design a data structure that supports: 1. `countInRoom(roomId) -> int` - Return how many people are currently in `roomId`. - Must be **O(1)** time. 2. `topKFastest(k) -> list[int]` - Return the **k fastest people**, where “faster” means: 1) People in **higher-numbered rooms** are faster (closer to the end). 2) For people in the **same room**, the one who **entered that room earlier** is faster. - Return fewer than k people if `k > P`. ### Example `R = 5`, `P = 5`. Operations: - `move(1)`, `move(2)`, `move(1)` State: - Person 1 is in room 2 - Person 2 is in room 1 - Others are in room 0 So: - `countInRoom(1) == 1`, `countInRoom(2) == 1` - `topKFastest(2)` returns `[1, 2]` (order can be `[1,2]` given the definition above). ## Complexity expectations Explain the time complexity of each operation and aim for efficient updates (especially avoiding scanning all people for `countInRoom` or `topKFastest`).

Quick Answer: This question evaluates understanding of dynamic data structure design and ordering semantics, specifically maintaining per-room FIFO arrival order, constant-time room counts, and efficient top-k retrieval based on hierarchical priorities.

Related Interview Questions

  • Design Hierarchical Permission Checks - Pinterest (medium)
  • Implement weighted random choice - Pinterest (medium)
  • Solve five hard algorithm problems - Pinterest
  • Sample a string by real-valued scores - Pinterest (hard)
  • Find First Prefix-Matching Word - Pinterest (medium)
Pinterest logo
Pinterest
Jan 12, 2026, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
5
0
Loading...

Problem

There are R rooms labeled 0..R-1 (in increasing order), and P people labeled 0..P-1.

  • Initially, all people are in room 0 .
  • Operation move(personId) moves that person from room i to room i+1 , if i < R-1 . (If already in the last room, they stay there.)
  • Within each room, maintain the arrival order of people (FIFO by the time they entered that room).

Design a data structure that supports:

  1. countInRoom(roomId) -> int
    • Return how many people are currently in roomId .
    • Must be O(1) time.
  2. topKFastest(k) -> list[int]
    • Return the k fastest people , where “faster” means:
      1. People in higher-numbered rooms are faster (closer to the end).
      2. For people in the same room , the one who entered that room earlier is faster.
    • Return fewer than k people if k > P .

Example

R = 5, P = 5.

Operations:

  • move(1) , move(2) , move(1)

State:

  • Person 1 is in room 2
  • Person 2 is in room 1
  • Others are in room 0

So:

  • countInRoom(1) == 1 , countInRoom(2) == 1
  • topKFastest(2) returns [1, 2] (order can be [1,2] given the definition above).

Complexity expectations

Explain the time complexity of each operation and aim for efficient updates (especially avoiding scanning all people for countInRoom or topKFastest).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Pinterest•More Machine Learning Engineer•Pinterest Machine Learning Engineer•Pinterest Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • 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.