Assign Each Request the Next Free Slot at or After Its Preference

Last updated: November 29, 2026

Quick Overview

There are num_slots serving slots numbered 0 to num_slots - 1. Requests arrive in order, each with a preferred slot p, and take the smallest free slot at or after p, or get -1 if none is free. Return each request's assigned slot.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


There are num_slots serving slots numbered 0 to num_slots - 1. Requests arrive in order, each with a preferred slot p, and take the smallest free slot at or after p, or get -1 if none is free. Return each request's assigned slot.

A Google screen question about a less familiar union-find use: pointer jumping to the next free index. It appears in contexts like batch scheduling on TPU pods or allocating ports. The interviewer checks that you replace a linear scan with a next-pointer union-find and argue the amortized bound.

What the Interviewer Expects
  • Notices that linear probing is O(n) per request in the worst case
  • Keeps a next-free pointer per slot with a sentinel
  • On assignment, links the slot to slot + 1
  • Uses path compression so chains of taken slots collapse
  • Handles exhausted capacity with -1
Key Topics to Cover
Union-Find
Next Pointer
Path Compression
Scheduling
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Requests can also release slots. Does this structure still work?
  • What if a request takes the nearest free slot in either direction?
  • How would you handle slots that wrap around circularly?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: num_slots and a list of preferred indices in range.
  • Each request gets the smallest free slot >= p.
  • Output: list of assigned slots or -1....
High-Level Design

Keep nxt[i], which leads toward the smallest free slot at or after i, with an extra sentinel index num_slots meaning none. Initially every slot points to itself. To serve a request, follow the p...


Submit Your Answer
Markdown supported