Recently, I gave a full-stack interview, and the question asked from JavaScript was like this 👇 🧠 let arrangedCards = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Joker", "Queen", "King"]; The user will give you shuffled cards, like: ["King", 4, 5, "A", "Joker"] ["Queen", 7, 5, 8, "King", 2] 🎯 Interview Task: Write a JavaScript method that arranges the shuffled cards by taking reference from arrangedCards. ✅ Expected Output: ["A", 4, 5, "Joker", "King"] [2, 5, 7, 8, "Queen", "King"] #JavaScript #FullStackInterview #CodingInterview #ProblemSolving #WebDevelopment #FrontendDeveloper
shuffled.sort((a, b) => arrangedCards.indexOf(a) - arrangedCards.indexOf(b)) .... this way you can solve this
We sort the shuffled cards by comparing their positions in the reference arrangedCards array
You can use any sort algorithm. Just create map of cards with card name as key and index as value and then use that for sorting when looping.