Sort the Jumbled Numbers - Problem

Imagine you have a secret code system where each digit 0-9 is mapped to a different digit. You're given an array of numbers and need to sort them based on how they would appear when decoded using this mapping system.

You are given:

  • A mapping array where mapping[i] = j means digit i should be replaced with digit j
  • An array nums containing integers to be sorted

Your task: Return the array nums sorted in ascending order based on their mapped values, but keep the original numbers in the result.

Example: If mapping = [8,9,4,0,2,1,3,5,7,6] and nums = [991,338,38]:

  • 991669 (9→6, 9→6, 1→9)
  • 338007 = 7 (3→0, 3→0, 8→7)
  • 3807 = 7 (3→0, 8→7)

Since both 338 and 38 map to 7, they maintain their relative order. Final result: [338, 38, 991]

Input & Output

example_1.py — Basic Sorting
$ Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
Output: [338, 38, 991]
💡 Note: 991 maps to 669 (9→6,9→6,1→9), 338 maps to 007=7 (3→0,3→0,8→7), 38 maps to 07=7 (3→0,8→7). Since 7 < 669, both 338 and 38 come before 991. Since 338 and 38 both map to 7, they maintain relative order.
example_2.py — All Same Mapping
$ Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
Output: [123, 456, 789]
💡 Note: Identity mapping means numbers stay the same: 789→789, 456→456, 123→123. Sorting gives [123, 456, 789].
example_3.py — Edge Case with Zero
$ Input: mapping = [1,0,2,3,4,5,6,7,8,9], nums = [0,10,1]
Output: [1, 0, 10]
💡 Note: 0 maps to 1, 10 maps to 01=1, 1 maps to 0. Sorted by mapped values: 1(→0), 0(→1), 10(→1). Since 0 and 10 both map to 1, they keep relative order.

Constraints

  • mapping.length == 10
  • 0 ≤ mapping[i] ≤ 9
  • All values in mapping[i] are unique
  • 1 ≤ nums.length ≤ 3 × 104
  • 0 ≤ nums[i] < 109

Visualization

Tap to expand
🕵️ Secret Agent Code Breaking Mission📡 INTERCEPTED TRANSMISSIONS99133838🔐 Cipher: [8,9,4,0,2,1,3,5,7,6]🔓 DECODING PHASE991 → 6699→6, 9→6, 1→9338 → 73→0, 3→0, 8→738 → 73→0, 8→7💡 Each message decoded exactly once - efficient!📊 INTELLIGENCE ANALYSISSort by decoded values: 7, 7, 669Maintain chronological order for identical priorities (stable sort)🎯 MISSION ACCOMPLISHED33838991
Understanding the Visualization
1
Cipher Setup
Create the decoding key: mapping[i] tells us what digit i becomes
2
Decode Messages
Transform each intercepted number using the cipher, digit by digit
3
Create Intelligence Report
Pair each original message with its decoded value and intercept order
4
Chronological Sorting
Sort by decoded values, maintaining original order for ties
5
Mission Complete
Return the original messages in their proper chronological sequence
Key Takeaway
🎯 Key Insight: By separating the decoding (mapping) phase from the sorting phase, we eliminate redundant calculations and achieve optimal O(n*m + n log n) time complexity while maintaining stable sort order.
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
52.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen