Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. 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.

Solve it in your browserRead the solution
EasyArrayGreedyAsked at 86 companies incl. Google, Amazon, Meta

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

1
2
3
4

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.

Start Free Trial

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

  1. For each index i with flowerbed[i] == 0, check that the left neighbor is empty or missing and the right neighbor is empty or missing.
  2. If so, plant (set it to 1, or just skip ahead to i + 2) and decrement n.
  3. Return true as soon as n reaches 0, and false if 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)

ApproachTimeSpace
Count runs of empty plotsO(n)O(1)
Greedy scanO(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 <= 0
▸Can 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

  • Container With Most WaterMediumTwo pointers from both ends
  • Gas StationMediumGreedy restart on deficit
  • Meeting Rooms IIMediumSweep line over start and end events
  • Maximum Matrix SumMediumParity invariant (greedy)

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • AI Interview Copilot
  • Coding Interview Copilot
  • AI Interview Assistant
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.