Given two lists, sort the first list and reorder the second list so that the elements remain in corresponding order.
1Times asked
Jun 2026Last seen
Jun 2026First seen
💡 Model Answer
To keep the correspondence between two lists while sorting the first, pair each element of the first list with its partner in the second list, sort the pairs, then separate them back. In Python:
python
list1 = [3, 1, 2]
list2 = ['c', 'a', 'b']
# Pair and sort
paired = sorted(zip(list1, list2), key=lambda x: x[0])
# Unzip
sorted_list1, sorted_list2 = zip(*paired)
print(list(sorted_list1)) # [1, 2, 3]
print(list(sorted_list2)) # ['a', 'b', 'c']This runs in O(n log n) time due to the sort, and uses O(n) extra space for the paired list. The same logic applies to any language: create an array of index/value pairs, sort by the first element, then rebuild the two lists from the sorted pairs.
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