Reported November 2023
ZipRecruitercounting

Compare Counts Around a Pivot

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

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

The mistake that sinks a first attempt on this ZipRecruiter OA, reported in November 2023, is counting the pivot itself. Values equal to the pivot belong to neither side, and people lump them into one bucket without thinking. The task is tiny: scan the array, count values strictly greater and strictly less than the pivot, then return "greater", "smaller", or "tie". It's a plain array counting problem. If your head goes blank under the timer, StealthCoder runs invisibly on screen and gives you the solution as a safety net. You probably won't need it, but it's there.

The problem

Given integers numbers and pivot, count values strictly greater than the pivot and strictly less than it. Values equal to the pivot belong to neither count.
Return "greater", "smaller", or "tie" according to which count is larger.

Function
compareAroundPivot(numbers: int[], pivot: int) → String

Examples
Example 1
numbers = [1,5,7,5,2]
pivot = 5
return = "smaller"
Two values are smaller, one is greater, and the two fives are ignored.
Example 2
numbers = [1,9]
pivot = 5
return = "tie"
One value lies on each side of the pivot.

Constraints
0 <= numbers.length <= 100000

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that there's no trick. One pass, two counters, O(n) time and O(1) space. Skip sorting, since it adds O(n log n) for no reason. The pitfalls are all about edges. Use strict comparisons, so a value equal to the pivot increments nothing. Check Example 1: [1,5,7,5,2] with pivot 5 gives two smaller, one greater, so the answer is "smaller", not "greater". Handle an empty array, which the constraints allow, by returning "tie" since both counts are zero. Return the exact lowercase strings. Don't compare to the array length or assume the pivot appears in the array. Write the loop, then trace both examples by hand before submitting. If you freeze on any of this during the live OA, StealthCoder is the hedge that reads the problem and hands you the clean version.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Compare Counts Around a Pivot 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

ZipRecruiter reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Compare Counts Around a Pivot FAQ

How hard is the ZipRecruiter compare counts around a pivot question?+

It's easy. One loop, two counters, one comparison at the end. The only way to fail is sloppy edge handling, like counting values equal to the pivot or forgetting the empty array. Treat it as a fast win and spend your care on correctness.

What's the trick to this problem?+

There isn't one beyond strict inequality. Increment the greater counter only when the value is above the pivot, and the smaller counter only when it's below. Values equal to the pivot do nothing. Then compare the two counters and return the matching string.

What should I return for an empty array?+

Return "tie". The constraints allow a length of zero, so both counts are zero and neither side is larger. A simple final comparison handles this naturally if you don't add special cases that break it.

Do I need to sort or use extra memory?+

No. Sorting costs O(n log n) and buys you nothing. A single pass with two integer counters runs in O(n) time and O(1) space, which is plenty for an array of up to 100000 elements.

How do I prepare for this in 48 hours?+

Write it once from scratch and trace both examples by hand. Then test three edge cases: empty array, all values equal to the pivot, and negative numbers. Also confirm your return strings match the spec exactly, lowercase and spelled right.

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

OA at ZipRecruiter?
Invisible during screen share
Get it