Home › Interview Questions › If L2 contains the values 1, 2, 3, 4, 5 and L1 con…

If L2 contains the values 1, 2, 3, 4, 5 and L1 contains 10, 20, 30, 40, 50, rearrange L1 so that the elements are ordered according to the relative ordering of L2.

🟡 Medium Coding Junior level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

Create a mapping from each value in L2 to its index, then reorder L1 by that index. For the example, the mapping is {1:0, 2:1, 3:2, 4:3, 5:4}. Sorting L1 by these indices yields [10, 20, 30, 40, 50]. The algorithm runs in O(n log n) time due to sorting and uses O(n) additional space for the map. If the lists are guaranteed to be permutations of each other, you can also build a result array of the same size and place each element of L1 at the mapped position, achieving O(n) time.

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 on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500