- Interview Questions
- /
- Problems
- /
- Can Place Flowers
Can Place Flowers
Decide whether n new flowers fit in a row of plots where no two flowers may be neighbors. A greedy scan where planting as early as possible is always safe. Solve it below in Python or JavaScript, then read the walkthrough.
Problem
A long garden bed is split into plots in a single row. You're given flowerbed, an array where 1 means a plot already has a flower and 0 means it's empty. The existing flowers never touch each other.
Flowers can't grow in adjacent plots. Given an integer n, return true if you can plant n more flowers in empty plots without any two flowers ending up next to each other, and false otherwise.
The two ends of the bed have no neighbor on the outside, so an empty end plot only needs its one inside neighbor to be empty.
Example 1
- Input
- flowerbed = [0, 0, 1, 0, 0], n = 2
- Output
- true
Plant at index 0 and index 4. Each end plot only has one neighbor.
Example 2
- Input
- flowerbed = [1, 0, 1, 0, 1], n = 1
- Output
- false
Every empty plot sits between two flowers.
Example 3
- Input
- flowerbed = [0, 0, 0, 0, 0], n = 3
- Output
- true
Plant at indices 0, 2 and 4.
Constraints
- 1 ≤ flowerbed.length ≤ 2 × 10⁴
- flowerbed[i] is 0 or 1, and no two 1s are adjacent.
- 0 ≤ n ≤ flowerbed.length
Practice
Runs 16 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.
Get asked Can Place Flowers in a real interview?
Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.
How to solve Can Place Flowers
Pattern: Greedy left-to-right placement
Walk the bed from left to right. Whenever a plot is empty and both of its neighbors are empty (treating the outside of the bed as empty), plant there right away.
Why is planting early never a mistake? A flower at plot i only blocks plots i - 1 and i + 1. Plot i - 1 was already passed over, and any plan that skips i to plant at i + 1 can shift that flower left to i without hurting anything to its right. So the greedy count is the maximum possible, and you can stop as soon as it reaches n.
Approach 1: Count runs of empty plots
Split the bed into maximal runs of zeros. An interior run of length z fits (z - 1) // 2 flowers, a run touching one end fits z // 2, and a bed that is entirely empty fits (z + 1) // 2. Add them up and compare with n. Linear, but the three cases are easy to get wrong.
Time O(n) · Space O(1)
Approach 2: Greedy scan
- For each index
iwithflowerbed[i] == 0, check that the left neighbor is empty or missing and the right neighbor is empty or missing. - If so, plant (set it to 1, or just skip ahead to
i + 2) and decrementn. - Return
trueas soon asnreaches 0, andfalseif the scan ends first.
Planting by marking the array keeps the left-neighbor check correct for the next plot. If you'd rather not modify the input, jump the index forward by two instead.
Time O(n) · Space O(1)
| Approach | Time | Space |
|---|---|---|
| Count runs of empty plots | O(n) | O(1) |
| Greedy scan | O(n) | O(1) |
Solution code
Try the practice editor first. Both solutions pass every test above.
Can Place Flowers solution in Python
def can_place_flowers(flowerbed: list[int], n: int) -> bool:
size = len(flowerbed)
i = 0
while i < size and n > 0:
if flowerbed[i] == 1:
i += 2 # the next plot is blocked
continue
left_empty = i == 0 or flowerbed[i - 1] == 0
right_empty = i == size - 1 or flowerbed[i + 1] == 0
if left_empty and right_empty:
n -= 1
i += 2
else:
i += 1
return n <= 0Can Place Flowers solution in JavaScript
function canPlaceFlowers(flowerbed, n) {
const size = flowerbed.length;
let i = 0;
while (i < size && n > 0) {
if (flowerbed[i] === 1) {
i += 2; // the next plot is blocked
continue;
}
const leftEmpty = i === 0 || flowerbed[i - 1] === 0;
const rightEmpty = i === size - 1 || flowerbed[i + 1] === 0;
if (leftEmpty && rightEmpty) {
n--;
i += 2;
} else {
i++;
}
}
return n <= 0;
}Common mistakes
- Indexing outside the array at the two ends instead of treating the missing neighbor as empty.
- Counting candidate plots without marking them, so two adjacent empty plots both get counted.
- Forgetting that n can be 0, which is always true even on a full bed.
- Using the interior formula for runs that touch the ends, which undercounts by one.
What interviewers look for
Say why greedy is safe in one sentence: "planting at the leftmost legal plot never blocks more than any other choice." Interviewers often ask for that justification on an easy problem to see whether you prove greedy choices or just hope.
Ask whether you're allowed to modify the input. If not, show the skip-ahead version, which reads the array without changing it. Mention the early exit once n hits 0.
Follow-up questions
What if flowers needed at least d empty plots between them?
The same greedy works: plant at the first empty plot whose nearest flowers on both sides are more than d plots away, then skip d plots ahead. Track the index of the last flower to check the left side in O(1).
What if the bed were circular, so the two ends are neighbors?
Handle the wraparound by solving two linear cases: one where the first plot stays empty (scan indices 1..n-1) and one where the last plot stays empty (scan 0..n-2), and take the better count.
How would you return the maximum number of flowers you could add?
Run the greedy scan to the end without the early exit and return the count. Greedy is optimal, so that count is the maximum.
Companies that ask Can Place Flowers
Can Place Flowers is tagged by 86 companies in our interview dataset. The score shows how often it comes up at each company.
- Mercedes Benz100%
- Lendingclub100%
- SOTI100%
- Redfin96%
- Crunchbase93%
- Dolby Laboratories90%
- Guardant Health89%
- Genesys87%
- Raytheon86%
- Nielseniq86%
- Mercado Libre85%
- Regeneron85%
- And Digital83%
- 3m83%
- Thg83%
- Mediatek82%
- Wizeline81%
- Sophos81%
- Ampere Computing79%
- Coffee Meets Bagel77%
- Nike76.3%
- Procter & Gamble76%
- Epic Games75%
- LinkedIn74.5%
and 62 more.
Practice next
Get live help in the real interview
Working solutions in real time during your actual interview. Invisible to screensharing.
Start Free TrialNo credit card required