HardArrayHash TableBinary SearchSortingPrefix SumOrdered SetUpdated Sep 2026

2251. Number of Flowers in Full Bloom

Asked at Databricks

Difficulty

Hard

Time

O((n + m) log n)

Space

O(n)

Pattern

Array

Companies

1

Problem

You get a list of flowers. Each flower is described by two days: flowers[i] = [start, end]. That flower is in full bloom on every day from start to end, including both of those days.

You also get a list people. Each person arrives on one specific day, people[i]. For every person, count how many flowers are blooming on the day they arrive.

Return an array answer of the same length as people, where answer[i] is the number of blooming flowers on the arrival day of person i. The days can be very large (up to 10^9), so you cannot loop over every day.

Which flowers are open on each visit day

flowersstartend01231637912413Flower 0 opens at day 1, flower 1 covers days 3 to 7,flower 3 covers days 4 to 13people237110123Person at index 1 arrives on day 3; two flowers areopen then
Each bar is one flower's bloom range. A person on day 3 stands inside two bars, so the answer is 2.

Constraints

  • ●1 <= flowers.length <= 5 * 10^4
  • ●flowers[i].length == 2
  • ●1 <= start_i <= end_i <= 10^9
  • ●1 <= people.length <= 5 * 10^4
  • ●1 <= people[i] <= 10^9

Examples

Example 1
Inputflowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11]
Output[1,2,2,2]
WhyOn day 2 only flower [1,6] is open. On day 3, flowers [1,6] and [3,7] are open. On day 7, [3,7] and [4,13] are open ([1,6] already ended). On day 11, [9,12] and [4,13] are open.
Example 2
Inputflowers = [[1,10],[3,3]], people = [3,3,2]
Output[2,2,1]
WhyOn day 3 both flowers are open, because [3,3] blooms only on day 3 and [1,10] covers it. On day 2 only [1,10] is open, so the count is 1.

Asked At

1 companies
CompanyDifficulty
DatabricksHardView all Databricks questions →

How to Think About It

1

Brute force: for each person, iterate all flowers and count how many are blooming — O(n * m) time.

2

Sort the bloom start days and bloom end days separately.

3

For a given day d, the number of blooming flowers = (flowers with start <= d) - (flowers with end < d).

4

Use binary search on the sorted start and end arrays to compute each count in O(log n).

5

Optimal: sort starts, sort ends, for each person binary search both arrays — O((n + m) log n) time.

Optimal Approach

Separate the bloom start days and bloom end days into two sorted arrays. For each person arriving on day d, use binary search to find the number of flowers that have started blooming (start <= d) and subtract the number that have already finished blooming (end < d). The result is the count of flowers in full bloom on day d. This gives O((n + m) log n) time where n is the number of flowers and m is the number of people.

Started minus ended

starts (sorted)start <= 313490123For day 3, two starts are <= 3, so two flowers haveopenedends (sorted)6712130123No end is smaller than 3, so no flower has finishedyetstarted = count(start <= d)ended = count(end < d)answer = started - ended
Count flowers that already started, then subtract flowers that already finished. What is left is blooming now.
ComplexityTime O((n + m) log n)Space O(n)Sorting the start and end days costs O(n log n), and each of the m people does two binary searches costing O(log n).

What Trips People Up in Real Interviews

1

Clarify whether the bloom interval is inclusive on both ends — it usually is.

2

Ask if people can arrive on the same day — yes, and each person's count is independent.

3

Use bisect_right for end days (flowers with end < d) and bisect_right for start days (flowers with start <= d).

4

Edge case: a flower that blooms on exactly one day (start == end) should be counted for that day.

5

Discuss the tradeoff between the sorting + binary search approach vs. a sweep line with a prefix sum.

Solution Code

O((n + m) log n) time
import bisect

def fullBloomFlowers(flowers, people):
    starts = sorted(f[0] for f in flowers)
    ends = sorted(f[1] for f in flowers)
    result = []
    for p in people:
        started = bisect.bisect_right(starts, p)
        ended = bisect.bisect_left(ends, p)
        result.append(started - ended)
    return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Number of Flowers in Full Bloom problem?

You are given a list of flowers, each with a bloom start day and bloom end day. A flower is in full bloom on any day between its start and end (inclusive). Given a list of people who each visit on a specific day, determine how many flowers are in full bloom on each person's visit day.

How do you solve Number of Flowers in Full Bloom?

The optimal approach runs in O((n + m) log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Number of Flowers in Full Bloom?

Number of Flowers in Full Bloom is asked at Databricks. It is a hard difficulty problem.

What are common mistakes on Number of Flowers in Full Bloom?
  • Clarify whether the bloom interval is inclusive on both ends — it usually is.
  • Ask if people can arrive on the same day — yes, and each person's count is independent.
  • Use bisect_right for end days (flowers with end < d) and bisect_right for start days (flowers with start <= d).
  • Edge case: a flower that blooms on exactly one day (start == end) should be counted for that day.
  • Discuss the tradeoff between the sorting + binary search approach vs. a sweep line with a prefix sum.