MEDIUMasked at 4 companies

Magnetic Force Between Two Balls

A medium-tier problem at 71% community acceptance, tagged with Array, Binary Search, Sorting. Reported in interviews at Roblox and 3 others.

Founder's read

Two balls with opposing charges repel each other. Given positions and forces, you need to find which pairs of balls can coexist without colliding. This problem combines sorting and binary search in a way that trips up candidates who jump straight to brute force. Amazon, Roblox, and Flipkart have all asked it. The trap is that a naive O(n^2) approach works for small inputs but fails on large datasets. The real solution requires you to see that once you sort by position, binary search becomes your tool for ruling out impossible pairs efficiently.

Companies asking
4
Difficulty
MEDIUM
Acceptance
71%

Companies that ask "Magnetic Force Between Two Balls"

If this hits your live OA

Magnetic Force Between Two Balls 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. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The key insight is that if two balls repel, one must move left and one must move right, or they collide. After sorting by position, you can use binary search to find the furthest ball to the left that a given ball can coexist with, then count valid pairs. Most candidates waste time on nested loops or miss that sorting unlocks a logarithmic search window. The problem sits at the intersection of Array, Sorting, and Binary Search not by accident. Common failure: treating force as symmetric without recognizing directional constraints. StealthCoder bridges this gap during a live assessment. If you freeze on the pattern, it surfaces the sorted-then-binary-search approach in seconds, invisible to the proctor, letting you write code instead of panicking.

Pattern tags

The honest play

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

Magnetic Force Between Two Balls 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. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Magnetic Force Between Two Balls interview FAQ

Is this harder than it looks?+

Yes. The 71% acceptance rate hides a subtle gotcha: sorting is obvious, but recognizing when and how to binary search on the sorted array separates solvers from blankers. The problem rewards structured thinking, not raw coding speed.

Do I really need binary search, or can I sort and check?+

Sort alone won't cut it on larger inputs. Binary search reduces your pair-checking from quadratic to linear. Candidates who skip this step often time out, which is why this problem appears on the harder half of Amazon's screening assessments.

How does this relate to real physics?+

It's a simplified model. The force value matters only to determine repulsion direction and whether collision happens. You're not simulating motion, just validating coexistence. Don't overthink the physics angle.

What's the most common mistake?+

Assuming all ball pairs can be checked independently without sorting first. That leads to an O(n^2) solution that looks correct but fails on large test cases. Sort first, then binary search the valid window.

Will this come up in my Flipkart or Roblox OA?+

It has been asked by both, plus Amazon. It's not a rare variant. If you haven't drilled the sorted-plus-binary-search pattern on position-based problems, this is the one that catches you. Blocking strategy: master it before your live assessment.

Want the actual problem statement? View "Magnetic Force Between Two Balls" 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.