EASYasked at 4 companies

Sort Integers by The Number of 1 Bits

A easy-tier problem at 79% community acceptance, tagged with Array, Bit Manipulation, Sorting. Reported in interviews at J.P. Morgan and 3 others.

Founder's read

You've got an array of integers and need to sort them by the count of 1 bits in their binary representation. It sounds algorithmic, but the trap is overthinking it. This problem shows up in assessments at J.P. Morgan, Goldman Sachs, Mapbox, and Accenture, and has a 79% acceptance rate because most candidates solve it the obvious way and move on. The real value is recognizing when a custom comparator beats a built-in sort, and how bit manipulation facts let you avoid extra helper functions. If this lands in your live OA and you blank on the pattern, StealthCoder solves it in seconds, invisible to the proctor.

Companies asking
4
Difficulty
EASY
Acceptance
79%

Companies that ask "Sort Integers by The Number of 1 Bits"

If this hits your live OA

Sort Integers by The Number of 1 Bits 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 trick is understanding that you're not sorting by value, you're sorting by a property of the value. Count the number of 1s in the binary form of each integer (using built-in popcount, bit-shifting in a loop, or Brian Kernighan's trick), then pass that count to your sort's comparator. Most candidates write a helper function to count bits, call it for every element during the sort, and move forward. The subtle second part: when two numbers have the same bit count, maintain their original relative order (stable sort) or keep their numerical order, depending on the problem spec. The common failure point is forgetting that the problem cares about both the count and the tiebreaker. If you hit this live and the bit-counting logic or comparator syntax slips your mind, StealthCoder reads the problem and surfaces a working solution in real time.

Pattern tags

The honest play

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

Sort Integers by The Number of 1 Bits recycles across companies for a reason. It's easy-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.

Sort Integers by The Number of 1 Bits interview FAQ

How do I count the number of 1 bits efficiently?+

Most languages have a built-in popcount or bitCount method. In Python, use bin(n).count('1'). In Java, Integer.bitCount(). In C++, __builtin_popcount(). These are O(1) or O(log n) depending on implementation. Avoid manual bit-shifting loops unless the language lacks a built-in; it's slower and error-prone.

Is this problem really asked at Goldman Sachs and J.P. Morgan?+

Yes. It appears frequently in reports from those firms and Accenture. At 79% acceptance, it's considered easy, so hiring teams use it to screen for clean code and understanding of custom sorting. Flub this and you signal carelessness.

What's the trick if two numbers have the same bit count?+

The problem usually wants you to maintain ascending numerical order as a tiebreaker. Sort by bit count first, then by value. Write your comparator to return the bit-count difference first, and only if that's zero, return the value difference. Missing this tiebreaker is the second-most common miss after bit-counting logic.

Do I need to use Array, Bit Manipulation, Sorting, and Counting all together?+

You touch all four topics, but don't overthink it. Array is your input and output structure. Bit Manipulation is the counting step. Sorting is the algorithm. Counting is the property you're sorting by. It's not four separate sub-problems; it's one problem that uses all four concepts in sequence.

What's the time and space complexity?+

Time is O(n log n) for the sort, where n is array length. The bit-counting per element is O(1) or O(log v) where v is the max value, negligible. Space is O(1) if you're allowed in-place sort, O(n) if you need to return a new array. Most online assessments expect the latter.

Want the actual problem statement? View "Sort Integers by The Number of 1 Bits" 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.