FastPrepBinary Cardinality Sorting
Problems/Binary Cardinality Sorting

Binary Cardinality Sorting

Patreon logoPatreon● EasyINTERNOA

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Patreon software engineer intern HackerRank online assessment. The report only names this task with a short title about binary cardinality and gives no other details.

We rebuilt it as the known HackerRank task of that name: sort numbers by how many 1 bits they have. The tie rule (smaller value first) and the limits are our practice assumptions.

Our version is about 60% the same as the reported question.

Given a list of non-negative integers nums, return the list sorted by binary cardinality in ascending order. Break ties by the decimal value in ascending order.

Note: the binary cardinality of a number is how many 1 bits its binary form has.

Function

cardinalitySort(nums: List<Integer>) → List<Integer>

Examples

Example 1

nums = [31,15,7,3,2]return = [2,3,7,15,31]

The 1-bit counts are 1, 2, 3, 4 and 5.

Example 2

nums = [1,2,3,4]return = [1,2,4,3]

1, 2 and 4 each have one 1 bit, so they come first in value order. 3 has two 1 bits.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9

More Patreon interview questions

  • Sum of Subarray RegionsOA · Seen Oct 2024
  • Three SumOA · Seen Sep 2024
  • Maximum Books From BundlesOA · Seen Apr 2023
  • Professors Teaching Outside Their DepartmentOA · Seen Apr 2023
  • Sliding-Window API Rate LimiterPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Maximize K-Element AND with an Increment BudgetAmazon · OA
  • Minimum Inversions 🍑TikTok · OATikTok interview questions
  • Closest NumbersHeadlands Technologies · OAHeadlands Technologies interview questions
  • Macaron Order with Set DiscountsShopify · OAShopify interview questions
  • Build the Task HierarchyPendo · OA
  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Competition Winner and Runner-Up (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
  • Rank Football TeamsCapital One · OACapital One interview questions
All Patreon interview questionsSee Patreon hiring insights
Saved
class Solution {
    public List<Integer> cardinalitySort(List<Integer> nums) {
        // write your code here
    }
}
Checking account…