Can You Place N Objects?
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates array traversal, adjacency-constraint reasoning, and greedy placement intuition, emphasizing handling of boundary conditions and counting feasible placements.
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.
Input: ([0, 0, 0, 0, 0], 3)
Expected Output: True
Explanation: You can place objects at indices 0, 2, and 4 for a total of 3.
Input: ([0, 1, 0], 1)
Expected Output: False
Explanation: Neither empty slot can be used because each is adjacent to an occupied slot.
Hints
- Try scanning from left to right and greedily placing an object whenever the current position is valid.
- Be careful with the first and last positions since one neighbor may be out of bounds.