Brightest position on a street
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's online assessment, last asked Sep 2025. It tests sweep line and prefix sums.
Street lamps stand on a number line. Each lamp is [p, r]: placed at p, it lights the closed interval [p - r, p + r]. The brightness of a point x is the number of lamps whose interval contains x. Return the smallest x with the highest brightness. When the maximum holds over a whole stretch, that is the stretch's left end. Do it in O(n log n) without walking the number line point by point.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
brightest_position(lights: list[list[int]]) -> int | None— returnsNonefor an empty list.
Example
brightest_position([[-2, 3], [1, 2]]) -> -1 # [-5, 1] and [-1, 3] overlap on [-1, 1]
brightest_position([[0, 1]]) -> -1 # the whole of [-1, 1] has brightness 1
brightest_position([[1, 2], [5, 2], [9, 1]]) -> 3 # [-1, 3] and [3, 7] touch at 3, which counts
Constraints
-
0 <= n <= 10^5,-10^9 <= p <= 10^9,0 <= r <= 10^9. - Intervals are closed: two lamps that only share an endpoint both light that point.
Clarifying questions
- Are the intervals closed? Do touching intervals overlap at the shared point?
- On a tie, the leftmost point?
- Are coordinates integers? What if radii are floats?
- What should an empty input return?
What a strong answer covers
- An event sweep:
+1atp - rand-1atp + r, sorted by coordinate. - The tie rule that makes closed intervals correct: at equal coordinates, process every start before any end.
- Recording the coordinate whenever the running count strictly exceeds the best so far, which yields the leftmost maximum.
- Alternatives: an ordered map of coordinate to delta, or putting the
-1atp + r + 1when coordinates are integers. O(n log n) time, O(n) space. - Large values:
p ± rcan reach 2 × 10^9, beyond 32-bit integers. - Tests: one lamp, identical lamps, lamps that only touch, radius 0.
Follow-ups
- Radii are floating point. How do you keep the tie rule exact?
- Return the whole brightest stretch
[left, right], not only its left end. - Lamps are added and removed over time, with queries in between.
Where it's asked
- Uber · Online assessment · Last asked Sep 202590 min · Two problems on a HackerRank-based platform, camera and screen share on · No AI
What it tests
sweep line · prefix sums
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
