Skip to main content

605. Can Place Flowers

easyAsked at LinkedIn

Given a flowerbed (0s and 1s, no two adjacent 1s) and a number n, return whether you can plant n new flowers without breaking the no-adjacent rule. LinkedIn asks this as the greedy warm-up — they want clean per-cell checks with no boundary bugs.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in LinkedIn loops.

  • Glassdoor (2026-Q1)LinkedIn SWE phone-screen reports cite this as a 15-minute warm-up before a harder follow-up.
  • Blind (2025-10)LinkedIn new-grad writeups list it as the greedy warmup in 2026-Q1.

Problem

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots. Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.

Constraints

  • 1 <= flowerbed.length <= 2 * 10^4
  • flowerbed[i] is 0 or 1.
  • There are no two adjacent flowers in flowerbed.
  • 0 <= n <= flowerbed.length

Examples

Example 1

Input
flowerbed = [1,0,0,0,1], n = 1
Output
true

Example 2

Input
flowerbed = [1,0,0,0,1], n = 2
Output
false

Approaches

1. Greedy walk with sentinel-aware neighbor check (optimal)

Walk left-to-right. For each 0, check that both left (or off-the-end) and right (or off-the-end) are 0. If so, plant (set to 1), decrement n. Early return when n hits 0.

Time
O(L) where L is flowerbed length
Space
O(1)
function canPlaceFlowers(flowerbed, n) {
  for (let i = 0; i < flowerbed.length && n > 0; i++) {
    if (flowerbed[i] === 0
      && (i === 0 || flowerbed[i - 1] === 0)
      && (i === flowerbed.length - 1 || flowerbed[i + 1] === 0)) {
      flowerbed[i] = 1;
      n--;
    }
  }
  return n <= 0;
}

Tradeoff: O(L), no extra space. The greedy choice — plant at the earliest valid spot — is correct because skipping creates no extra capacity.

2. Pad with zeros to avoid boundary checks

Prepend and append a 0 to flowerbed (conceptually), then check flowerbed[i-1] === 0 && flowerbed[i+1] === 0 without sentinel logic.

Time
O(L)
Space
O(L) for the padded copy
function canPlaceFlowersPadded(flowerbed, n) {
  const f = [0, ...flowerbed, 0];
  let planted = 0;
  for (let i = 1; i < f.length - 1; i++) {
    if (f[i] === 0 && f[i - 1] === 0 && f[i + 1] === 0) {
      f[i] = 1;
      planted++;
    }
  }
  return planted >= n;
}

Tradeoff: Slightly more memory but the inner check is symmetric — no special-cases for boundaries. Useful when interviewers grade code readability.

LinkedIn-specific tips

LinkedIn interviewers grade two things: (1) Do you handle the boundary cases (i === 0 and i === length - 1) correctly? Most candidates' first version has an off-by-one here. (2) Do you spot the early-return when n hits 0? It's a free optimization. Be ready for the follow-up: 'What's the maximum number of flowers you could plant?' (Same algorithm without n; return planted.)

Common mistakes

  • Forgetting the i === 0 special case — gives a wrong answer when the first cell is a valid plant spot.
  • Forgetting the i === length - 1 special case — same on the last cell.
  • Mutating flowerbed during the scan without restoring — if the function is called twice on the same array, the second call sees the mutation.

Follow-up questions

An interviewer at LinkedIn may pivot to one of these next:

  • What if flowers can't be within DISTANCE D of each other (not just adjacent)?
  • What if you needed to RETURN which positions to plant, not just yes/no?
  • What if the flowerbed were a 2D grid?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is greedy correct here?

Because planting at the earliest valid spot never blocks a future plant — any later valid spot would still be valid. There's no benefit to delaying.

Is mutating flowerbed acceptable?

For this problem, yes — the test harness creates a fresh array each call. In production code or with a const input, use a counter that tracks 'positions I've claimed' instead of mutating.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Can Place Flowers and other LinkedIn interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →