Requirement: sort the first list and reorder the second list to keep corresponding values.
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