Binary Cardinality Sorting
Updated
Problem statement
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
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