Decide whether every car can park given preferred spots and forward-only overflow

Read the full interview experience this question came from →

Quick Overview

A coding question about cars arriving one at a time on a one-way street, each trying its preferred parking spot first and moving only forward when that spot is taken. You decide whether every car manages to park, which tests careful rule-following and efficient handling of spot occupancy for large inputs.

Decide whether every car can park given preferred spots and forward-only overflow

Company: Mercor

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

A one-way street has `n` parking spots in a row, numbered `0` to `n - 1` in the direction of travel. Cars arrive one at a time, in the order given by `preferences`, and car `i` prefers spot `preferences[i]`. Each car tries its preferred spot first and can only move forward from there. Decide whether every car manages to park. ### Function Signature ```python def can_all_park(n: int, preferences: list[int]) -> bool: ``` ### Rules - All spots start empty, and a parked car never moves. - An arriving car drives to its preferred spot. If that spot is empty, the car parks there. - Otherwise, the car continues forward and parks in the first empty spot with a larger index. - A car never moves backward. If there is no empty spot at or after its preferred spot, the car fails to park. - Return `True` if every car parks and `False` if at least one car fails. An empty `preferences` list returns `True`. ### Constraints - `1 <= n <= 10^5` - `0 <= len(preferences) <= 10^5` - `0 <= preferences[i] <= n - 1` - All values fit in a 32-bit signed integer. ### Examples **Example 1** ```text Input: n = 3, preferences = [0, 0, 1] Output: True ``` Car 0 parks in spot 0. Car 1 finds spot 0 taken and parks in spot 1. Car 2 finds spot 1 taken and parks in spot 2. **Example 2** ```text Input: n = 3, preferences = [1, 1, 2] Output: False ``` Car 0 parks in spot 1, and car 1 moves forward to spot 2. Car 2 finds spot 2 taken and has no spot after it, so it fails even though spot 0 is empty. **Example 3** ```text Input: n = 4, preferences = [2, 0] Output: True ``` Car 0 parks in spot 2 and car 1 parks in spot 0; spots 1 and 3 stay empty.

Overview: A coding question about cars arriving one at a time on a one-way street, each trying its preferred parking spot first and moving only forward when that spot is taken. You decide whether every car manages to park, which tests careful rule-following and efficient handling of spot occupancy for large inputs.

Read the full Mercor Machine Learning Engineer interview experience this question came from

|Home/Coding & Algorithms/Mercor
Mercor logo
Mercor
Sep 25, 2026
mediumMachine Learning EngineerOnsiteCoding & Algorithms
2
0

A one-way street has n parking spots in a row, numbered 0 to n - 1 in the direction of travel. Cars arrive one at a time, in the order given by preferences, and car i prefers spot preferences[i]. Each car tries its preferred spot first and can only move forward from there. Decide whether every car manages to park.

Function Signature

def can_all_park(n: int, preferences: list[int]) -> bool:

Rules

  • All spots start empty, and a parked car never moves.
  • An arriving car drives to its preferred spot. If that spot is empty, the car parks there.
  • Otherwise, the car continues forward and parks in the first empty spot with a larger index.
  • A car never moves backward. If there is no empty spot at or after its preferred spot, the car fails to park.
  • Return True if every car parks and False if at least one car fails. An empty preferences list returns True .

Constraints

  • 1 <= n <= 10^5
  • 0 <= len(preferences) <= 10^5
  • 0 <= preferences[i] <= n - 1
  • All values fit in a 32-bit signed integer.

Examples

Example 1

Input:  n = 3, preferences = [0, 0, 1]
Output: True

Car 0 parks in spot 0. Car 1 finds spot 0 taken and parks in spot 1. Car 2 finds spot 1 taken and parks in spot 2.

Example 2

Input:  n = 3, preferences = [1, 1, 2]
Output: False

Car 0 parks in spot 1, and car 1 moves forward to spot 2. Car 2 finds spot 2 taken and has no spot after it, so it fails even though spot 0 is empty.

Example 3

Input:  n = 4, preferences = [2, 0]
Output: True

Car 0 parks in spot 2 and car 1 parks in spot 0; spots 1 and 3 stay empty.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...