Reported August 2024
Salesforcetwo pointers

Find Num of Perfect Pairs

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

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

The mistake that sinks a first attempt at this Salesforce OA, reported in August 2024, is writing the O(n^2) double loop and checking the ugly min/max condition literally. It passes the sample, then times out on the hidden tests. The problem looks like a messy inequality, but it collapses into a clean sorting and two-pointer count. If you blank on the simplification during the live assessment, StealthCoder is the invisible safety net that reads the problem and hands you the reduction. Better to walk in already knowing it.

The problem

Given an array, find the number of perfec pairs.
(x, y) is a perfect pair if satisfies:
min(|x-y|, |x+y|) <= min (|x|, |y|) and max(|x-y|, |x+y|) > max(|x|, |y|)

Function
findNumberOfPerfectPairs(arr: int[]) → int

Examples
Example 1
arr = [2, 5, -3]
return = 2
out of (2, 5), (5, -3) and (2, -3), (2, -3) and (5, -3) satisfy both the conditions. Therefore, the output is 2.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Take absolute values. For a pair with |x| = a <= b = |y|, the two quantities |x-y| and |x+y| are just b-a and a+b in some order. So min is b-a and max is a+b. The first condition becomes b-a <= a, which is b <= 2a. The second becomes a+b > b, which means a > 0. So zeros never pair. Convert everything to absolute values, drop zeros, sort, then for each index count how many later elements are <= 2*a using two pointers or binary search. That's O(n log n). The pitfall is skipping the absolute-value step and treating signs separately, or counting zeros because the first condition holds. Check your reduction against the sample: [2,5,-3] becomes [2,3,5], and the valid pairs are (2,3) and (3,5). That gives 2. If the live OA freezes you, StealthCoder can supply this reduction in real time.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Find Num of Perfect Pairs 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Salesforce reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find Num of Perfect Pairs FAQ

What's the trick to Find Num of Perfect Pairs?+

Reduce to absolute values. With a <= b as the absolute values, the pair is perfect exactly when a > 0 and b <= 2a. Signs stop mattering. Then it's a sorted-array counting problem instead of a brute-force pair check.

How hard is this one really?+

The code is easy once you see the reduction, maybe 10 lines. The difficulty is realizing the scary min/max condition simplifies. Spend your first few minutes testing a=2, b=3 and a=2, b=5 by hand and the pattern shows up.

Do zeros count as perfect pairs?+

No. If one value is 0, then a = 0 and the max condition becomes b > b, which is false. So filter zeros out before counting. Forgetting this is a classic wrong-answer on hidden tests.

What complexity should I aim for?+

O(n log n) from sorting the absolute values, then a two-pointer sweep or binary search for the largest index with value <= 2*a. The O(n^2) brute force may pass small inputs but fail large ones, so don't submit it.

How do I prepare in 48 hours for this kind of question?+

Practice problems where you simplify a condition algebraically, then sort and count with two pointers. Write this one from scratch twice. Test edge cases: zeros, duplicates, negatives, and equal absolute values like 3 and -3.

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

OA at Salesforce?
Invisible during screen share
Get it