Reported January 2024
Amazonmath

Number of Suitable Locations

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon's January 2024 OA asked you to count valid warehouse locations on a number line. Given delivery centers at specific positions and a max travel distance d, you need to find how many integer points exist where every center can reach that point without exceeding d. This is a range-intersection problem disguised as logistics. StealthCoder can surface the core insight if you freeze up on pattern recognition during the live OA.

The problem

✍️ Feel free to see the Image Source section at the very bottom of the page for the original problem statement 🐘 Amazon, with its vast network of delivery centers scattered across the globe, operates on a massive number line stretching from -10⁹ to 10⁹. Imagine each center as a beacon along this number line, positioned at specific points known as center[i]. Now, Amazon is on a mission to find perfect warehouse spots, places where products from every delivery center can be gathered without exceeding a maximum travel distance, d. These warehouse spots, or "suitable locations," must be within reach of all delivery centers, ensuring that the total travel distance for bringing products to any one point doesn't go beyond d. The task is to figure out how many such ideal locations exist on the number line, where the journey for all products is within this limit. 🌷 Can't thank engouth to the legendary GG of Error-Free Excellence - 🌟 spike 🌟! 🌷

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: a location is suitable if and only if the maximum distance from that location to any delivery center is at most d. Rephrase: for a candidate position p, max(|center[i] - p|) must be <= d for all centers. This means p must lie in the intersection of ranges [center[i] - d, center[i] + d] for every center. Find the leftmost and rightmost valid integer in this intersection. If the centers span a range wider than 2*d, no valid location exists. Otherwise, count the integers in the overlap. StealthCoder helps you validate logic when you're unsure whether off-by-one errors are killing your solution.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Number of Suitable Locations 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Suitable Locations FAQ

Do I need to check every integer on the number line?+

No. The valid region is a contiguous interval. Find the intersection of all ranges [center[i] - d, center[i] + d]. The leftmost valid point is max(center[i]) - d. The rightmost is min(center[i]) + d. Count integers in that span.

What happens if the centers span more than 2*d units?+

No suitable location exists. If max(center) - min(center) > 2*d, the farthest centers are unreachable from any single point. Return 0.

Is this actually a hard problem or a trap?+

It's a medium-difficulty problem that sounds harder than it is. Once you realize it's interval intersection, the solution is linear. The narrative about warehouses is theater. Focus on the geometry.

What's the edge case I'll forget?+

When left > right after computing the valid interval, return 0. Also handle negative centers and large coordinate ranges without overflow. Use 64-bit integers if your language requires it.

Can I solve this in one pass?+

Yes. Find min and max of centers in one pass. Compute left = max(centers) - d and right = min(centers) + d. Return max(0, right - left + 1).

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it