Can You Place N Objects?
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given a binary array `slots` representing a row of locations. `1` means the location is already occupied, and `0` means it is empty.
You want to place `n` new objects into the row. A new object can be placed at index `i` only if:
- `slots[i] == 0`
- the left neighbor is either out of bounds or equal to `0`
- the right neighbor is either out of bounds or equal to `0`
Determine whether it is possible to place at least `n` new objects without moving any existing ones.
Example:
- For `slots = [1, 0, 0, 0, 0, 1]`, the maximum number of new objects that can be placed is `1`.
- Therefore, the answer is `true` when `n = 1`, and `false` when `n = 2`.
Return a boolean value.
Quick Answer: This question evaluates array traversal, adjacency-constraint reasoning, and greedy placement intuition, emphasizing handling of boundary conditions and counting feasible placements.
You are given a binary array `slots` representing a row of locations. A value of `1` means the location is already occupied, and `0` means it is empty.
You want to place `n` new objects into the row. A new object can be placed at index `i` only if:
- `slots[i] == 0`
- the left neighbor is either out of bounds or equal to `0`
- the right neighbor is either out of bounds or equal to `0`
Determine whether it is possible to place at least `n` new objects without moving any existing ones.
For example, for `slots = [1, 0, 0, 0, 0, 1]`, the maximum number of new objects that can be placed is `1`, so the answer is `True` when `n = 1` and `False` when `n = 2`.
Constraints
- 0 <= len(slots) <= 20000
- Each element of `slots` is either 0 or 1
- 0 <= n <= len(slots)
Examples
Input: ([1, 0, 0, 0, 0, 1], 1)
Expected Output: True
Explanation: You can place one new object at index 2. That satisfies the requirement of placing at least 1 object.
Input: ([1, 0, 0, 0, 0, 1], 2)
Expected Output: False
Explanation: Only one valid placement is possible in this row, so placing 2 objects is impossible.
Input: ([0], 1)
Expected Output: True
Explanation: The only slot is empty and has no neighbors, so one object can be placed there.
Input: ([], 0)
Expected Output: True
Explanation: Placing zero objects is always possible, even when the row is empty.