HomeInterview QuestionsGiven two lists L1 and L2 of equal length, rearran…

Given two lists L1 and L2 of equal length, rearrange L1 so that its elements follow the order defined by L2.

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

💡 Model Answer

To reorder L1 according to the order of L2, first build a dictionary that maps each element of L2 to its index. Then create a new list by sorting the elements of L1 based on the index retrieved from that dictionary. In Python:

python
def reorder(l1, l2):
    index_map = {value: i for i, value in enumerate(l2)}
    return sorted(l1, key=lambda x: index_map[x])

This assumes that every element in L1 appears in L2. The time complexity is O(n log n) due to the sorting step, and the space complexity is O(n) for the mapping. If the lists are already sorted or you need a stable order, you can use a stable sort or a counting approach to achieve 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