Home › Interview Questions › Given two lists as input, rearrange the first list…

Given two lists as input, rearrange the first list to match the order of the second list.

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

💡 Model Answer

The solution is identical to question 1. Build a mapping from each element in the second list to its position, then sort the first list using that mapping as the key. Complexity remains O(n log n) for sorting and O(n) extra space for the map. Example in JavaScript:

js
function reorder(l1, l2) {
  const map = new Map(l2.map((v, i) => [v, i]));
  return l1.slice().sort((a, b) => map.get(a) - map.get(b));
}

If the lists contain duplicates, the mapping should store a queue of indices to preserve relative order.

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