Dsa
Sorting
Medium
Targeted Hamming Distance Sort
Given a 'Target' integer and a list of integers, sort the list based on the Hamming distance (number of differing bits in binary) between each integer and the target. If two integers have the same Hamming distance to the target, sort them by their population count (total number of set bits). If they still tie, sort them by their actual numeric value in ascending order.
Example:
Input: '7 8 1' (Target is 7, list is [8, 1])
Binary 7: 0111, Binary 8: 1000, Binary 1: 0001
Distance(7,8) is 4 bits, Distance(7,1) is 2 bits.
Output: '1 8'
Key concepts
sortingbitwisehamming-distance
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now