Home › Interview Questions › Requirement: sort the first list and reorder the s…

Requirement: sort the first list and reorder the second list to keep corresponding values.

🟔 Medium Coding Junior level
1Times asked
Jun 2026Last seen
Jun 2026First seen

šŸ’” Model Answer

The solution is identical to question 1. Pair each element of the first list with its counterpart in the second list, sort the pairs by the first element, then separate them back into two lists. Complexity remains O(n log n) time and O(n) space. Example in JavaScript:

js
const list1 = [3, 1, 2];
const list2 = ['c', 'a', 'b'];
const paired = list1.map((v, i) => [v, list2[i]]);
paired.sort((a, b) => a[0] - b[0]);
const sortedList1 = paired.map(p => p[0]);
const sortedList2 = paired.map(p => p[1]);
console.log(sortedList1); // [1, 2, 3]
console.log(sortedList2); // ['a', 'b', 'c']

This keeps the relationship between the two lists intact after sorting.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

šŸŽ¤ Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500