← Questions
Every ordering of N students in a line, then in a circle
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked Apr 2026. It tests backtracking and permutations.
Given n students with distinct ids, return every way they can stand in a line. Then the follow-up: they stand in a circle, where rotations of the same arrangement count as one (a reversed circle still counts as different).
Part 1: in a line
-
line_orders(students: list[int]) -> list[list[int]]— every permutation, generated by picking the next unused student from left to right in the input, so the output order is fixed.
line_orders([1, 2, 3]) -> [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
line_orders([7]) -> [[7]]
line_orders([]) -> [[]]
Part 2: in a circle
-
circle_orders(students: list[int]) -> list[list[int]]— one list per distinct circle, each written starting from the first input student.
circle_orders([1, 2, 3]) -> [[1, 2, 3], [1, 3, 2]]
circle_orders([1, 2, 3, 4]) -> [[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4],
[1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2]]
Constraints
-
0 <= n <= 8for the line,n <= 9for the circle (output is factorial in size).
Clarifying questions
- Do mirror images of a circle count as different?
- Is there a required output order?
- Return the arrangements, or only how many there are?
What a strong answer covers
- Backtracking with a used-flag per student, or swap-based permutation.
- For the circle, fixing one student in place and permuting the rest:
(n - 1)!circles. - O(n · n!) time, dominated by the output.
- Edge cases: zero and one student.
Follow-ups
- Count the arrangements without listing them, including when mirror images count as the same (
(n - 1)! / 2forn >= 3). - Two given students must not stand next to each other.
- Generate the next arrangement in lexicographic order without recursion.
Where it's asked
- Amazon · Technical phone screen · Last asked Apr 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
backtracking · permutations
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
