Can Place Flowers

EasyGreedygreedy scan

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a long, narrow garden bed represented by the integer array flowerbed, where each element is either 0 (the plot is empty) or 1 (the plot already holds a flower). Flowers cannot be planted in two adjacent plots — any newly planted flower must have empty plots (or the edge of the bed) directly to its left and right. The existing flowers in flowerbed already satisfy this rule. Given an integer n, determine whether you can plant **at least** n new flowers in the empty plots without violating the no-adjacent rule. Return true if it is possible and false otherwise.

Examples

Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
The plot at index 2 has empty neighbors on both sides, so one flower fits there.
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Only one new flower can be placed (at index 2), which is short of the required two.
Input: flowerbed = [0,0,0], n = 2
Output: true
Plant at index 0 and index 2; they are not adjacent to each other or any existing flower.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.