Generate all permutations of a string
Sigiloso
They expected me to write some code on the whiteboard. I felt a bit rusty and used a greedy approach (O(n^n)) to stay on the safe side. Then I explained that there's a more efficient (O(n!)) way of doing that which takes me more time to come up with and can be found in almost any textbook. Anyways, the following is a simple JS implementation (O(n!)) in case anyone is interested: const perm = (acc, str) => { if (str === "") { console.log(acc) return; } const chars = [...str]; for (let i = 0; i < chars.length; i += 1) { const firstChar = chars[i]; const rest = [...chars.slice(0, i), ...chars.slice(i + 1)] perm(acc + firstChar, rest.join('')) } } perm("", "abcd");