MEDIUMasked at 1 company

Sum of Distances

A medium-tier problem at 31% community acceptance, tagged with Array, Hash Table, Prefix Sum. Reported in interviews at BNY Mellon and 0 others.

Founder's read

Sum of Distances is the kind of problem that looks straightforward until you realize the brute force won't cut it. You're asked to compute distances, so you reach for nested loops, and boom - you'll timeout on anything larger than small inputs. The 31% acceptance rate reflects candidates who didn't see the prefix sum trick before the timer ran out. BNY Mellon has asked this one. If it shows up in your assessment and you're not immediately seeing the pattern, StealthCoder will surface a working solution in seconds while the proctor sees nothing.

Companies asking
1
Difficulty
MEDIUM
Acceptance
31%

Companies that ask "Sum of Distances"

If this hits your live OA

Sum of Distances is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.

Get StealthCoder
What this means

The trap is computing each element's contribution naively. The real move is recognizing that you can precompute contributions using a single pass and arithmetic, not iteration. Prefix sums let you track how many elements are to the left or right of each position and their cumulative distance in constant time per element. Most candidates either TLE on brute force or struggle to set up the prefix sum state correctly. The pattern involves counting and summing simultaneously as you traverse the array. This is where StealthCoder becomes your insurance: if the prefix sum pattern doesn't click during the live OA, you get a correct implementation instantly, invisible to the screen share.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Sum of Distances recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sum of Distances interview FAQ

Is this still actually asked at real companies?+

Yes. BNY Mellon has reported asking it. The 31% acceptance rate suggests it's in active rotation. It's not a daily leetcode grind problem, but it does appear in online assessments. If you see it, you're on the harder side of medium difficulty.

What's the main trick I'm missing if I timeout?+

You're probably iterating over distances multiple times. The trick is computing each position's total distance in one pass using prefix sums and element counts. Instead of comparing every pair, track cumulative sums and count of elements to the left and right of each index.

How does this relate to the Array and Hash Table topics?+

Array is your primary tool. Hash Table helps if you need to handle sparse or weighted data, but the core solution uses prefix sums on an array. Some variants require counting occurrences or grouping, which is where hash tables enter.

Can I solve this without prefix sums?+

Technically yes, but not within time constraints. Brute force comparing every pair is O(n squared). Prefix sum and single-pass counting drops you to O(n), which is the expected solution.

Is this harder than typical medium problems?+

The 31% acceptance rate puts it on the harder end of medium. It's not algorithmic complexity that kills you, it's recognizing the prefix sum optimization fast enough before timeout. Standard practice drills don't always cover this pattern.

Want the actual problem statement? View "Sum of Distances" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.