Get Min Distance (AMZ CN)
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're looking at Amazon's take on the classic pairing problem, reported in December 2023. You have two arrays of positions (data centers and servers), and you need to match each data center to a unique server such that the sum of absolute distances is minimized. This isn't a search problem. It's about recognizing that a greedy sort-and-pair approach works here. If you blank on the OA, StealthCoder reads the problem and walks you through the greedy logic in real time.
The problem
\ note - See the Problem Source at the vely bottom of the page for the original problem description~\ \ In a realm where data centers and servers flourished along a mystical one-dimensional line, a wise ruler sought to optimize their connections. Each data center needed to establish a bond with a server, minimizing the lag defined as the absolute distance between their coordinates. Tasked with this quest, the ruler gathered the bravest minds to decipher the best pairings from the enchanted positions of the data centers and servers. Together, they embarked on a journey to forge connections that would ensure the smallest lag possible, bringing harmony to the kingdom's network and enhancing its magical efficiency.\ \ ❀⊱ 🌺 Credit to Tong and txr and aikay! 🌺 ⊰❀\
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that sorting both arrays and pairing them in order gives the optimal result. Once both arrays are sorted, the smallest data center pairs with the smallest server, the next smallest with the next smallest, and so on. This greedy pairing minimizes total distance. The proof is intuitive: any crossing pairing would increase the sum. The catch is implementation: you need to track which servers have been used (a set or boolean array works), then iterate through data centers and assign each to the nearest available server in order. Common pitfall: trying a brute force assignment without sorting first. StealthCoder spots this pattern immediately and confirms the greedy approach.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Get Min Distance (AMZ CN) 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 StealthCoderRelated leaked OAs
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.
Get Min Distance (AMZ CN) FAQ
Do I have to match every data center to a server?+
Yes. The problem states each data center needs to establish a bond with a server. Every data center gets exactly one unique server. If there are more servers than data centers, some servers go unused. Think one-to-one pairing, not one-to-many.
Is this a dynamic programming problem?+
No. Don't overthink it. Sort both arrays, pair greedily in order, sum the distances. O(n log n) for sorting, O(n) for pairing. DP would be overkill and slower. The greedy approach is optimal here.
What if there are more data centers than servers?+
The problem implies equal counts or enough servers. If the input allows mismatches, the problem statement would clarify. For Amazon's version, assume n data centers and n servers. Read the constraints carefully on the OA.
How do I avoid pairing the same server twice?+
After sorting both arrays, iterate through data centers in order. For each, assign it to the first available server (check a set or boolean array of used indices). This ensures no duplicates and maintains the greedy optimal order.
Can I solve this in under 10 minutes?+
Yes. Sort, pair, sum. Code is about 15-20 lines. The hard part is recognizing the pattern. Once you see it's greedy, implementation is straightforward. Practice the sort-and-pair mental model now.