Place a Lamp to Cover the Most Objects
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reported this one in September 2024, and the whole problem hinges on one thing: the objects are already sorted, so you don't need anything fancier than two indices moving across an array. You pick a lamp coordinate, it lights everything within radius, and you want the spot that lights the most objects, smallest coordinate on ties. If your OA invite is a day or two out, this is the shape to recognize. It's a sliding window in disguise. And if you blank on the tie-break math mid-assessment, StealthCoder runs invisibly on screen as a safety net.
The problem
objects contains unique coordinates in strictly increasing order. A lamp at integer coordinate x illuminates every object whose distance from x is at most radius. Return an integer lamp coordinate that illuminates the most objects, breaking ties by the smallest coordinate. Function bestLampCoordinate(objects: int[], radius: int) → int Examples Example 1 objects = [-5,3,4,9] radius = 5 return = -1 The window [-5,4] is covered by lamp coordinate -1 and contains three objects. Example 2 objects = [7] radius = 2 return = 5 The smallest lamp coordinate covering the object is five. Constraints 1 <= objects.length <= 100000 -1000000000 <= objects[i] <= 1000000000 0 <= radius <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: a lamp covers a contiguous run of objects, and a run objects[i..j] is coverable only if objects[j] - objects[i] <= 2*radius. So slide a window with two pointers, shrink from the left when the span exceeds 2*radius, and track the max count. For the coordinate, the smallest lamp that covers a window starting at i is objects[j] - radius, since it must reach the rightmost object. Example 1 shows it: window [-5,4], 4 - 5 = -1. Pitfalls: ties. A later window with equal count could give a larger coordinate, so only update on strictly greater count, and the first best window gives the smallest answer. Also, 2*radius reaches 2e9, so watch integer overflow in languages with 32-bit ints. Radius 0 means each object alone. StealthCoder is your hedge in the live OA if the window math or the overflow case slips.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Place a Lamp to Cover the Most Objects cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Place a Lamp to Cover the Most Objects FAQ
What's the trick in the ZipRecruiter lamp problem?+
Treat it as a sliding window on the sorted array. A set of objects is coverable by one lamp if the gap between its first and last object is at most 2*radius. Find the widest valid window, then compute the coordinate from its right end minus radius.
How do I get the smallest lamp coordinate on ties?+
For any window ending at objects[j], the smallest lamp that reaches it is objects[j] - radius. Windows are scanned left to right, so coordinates only increase. Update your best only when the count is strictly greater, and the earliest best window wins automatically.
Do I need a heap, map, or segment tree?+
No. The input is already sorted in strictly increasing order, so two pointers do it in O(n) time and O(1) space. Binary search per object also works at O(n log n), but the sliding window is simpler and less error-prone under pressure.
What edge cases break most solutions?+
Overflow is the big one. Coordinates reach 1e9 and 2*radius reaches 2e9, which overflows a 32-bit int. Also check a single object (Example 2 gives object minus radius, so 5), radius 0, and negative coordinates where the answer can be negative.
How do I prep for this in 48 hours?+
Write the sliding window template on sorted arrays until it's automatic. Then do the same problem with a binary search variant to compare. Hand-trace Example 1 and Example 2 to confirm the coordinate formula. Skip heavy data structures, this one doesn't need them.