Fewest tray rotations to eat a three-course meal
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's agentic coding (beta) round, last asked Mar 2026. It tests dynamic programming and circular arrays.
A round table has seats positions numbered clockwise from 0, and a customer sits at position customer. A rotating tray holds one dish slot in line with each seat; dishes[i] is the dish that starts at position i: E (starter), P (main), D (dessert) or N (nothing). One rotation turns the tray one position either way. The customer must eat one starter, then one main, then one dessert, each when that dish is in front of them; eating costs nothing. Return the fewest rotations needed to finish the meal.
Interface
Unless you agree on a different interface with the interviewer, assume:
min_rotations(seats: int, customer: int, dishes: str) -> int
Example
min_rotations(6, 0, "NENDPE") -> 3
# bring the starter from 5 (1 rotation), then the main from 4 (1), then the dessert from 3 (1).
# The starter at 1 is just as close, but then the meal costs 1 + 3 + 1 = 5.
min_rotations(3, 0, "EPD") -> 2 # starter already in front; main and dessert one turn each
min_rotations(5, 2, "PNEDN") -> 4 # starter at 2 (0), main at 0 (2), dessert at 3 (2)
Constraints
-
3 <= seats < 20,0 <= customer < seats,len(dishes) == seats. - At least one dish of each course is on the tray.
Clarifying questions
- Is the cost of moving from dish
ato dishbthe shorter way around the circle? - Does a dish disappear once eaten, and could it matter (it can't here, since the courses differ)?
- Can the customer skip past a dish of the right course without eating it?
What a strong answer covers
- The cost of a meal is
dist(customer, e) + dist(e, p) + dist(p, d)with circular distancemin(|a - b|, seats - |a - b|). - Why greedy (nearest starter, then nearest main) fails, shown with a counterexample like the first example.
- Either trying every (starter, main, dessert) triple, at most O(seats^3), which is tiny here, or a three-layer DP: best cost to finish the main at each main position, then the dessert, O(seats^2).
- Tests: a dish already in front, ties between directions, the wrap-around between the last and first position.
Follow-ups
- The meal has
kcourses in a fixed order. Generalize the DP and give its complexity. - Several customers share the tray and each rotation moves it for everyone. What changes?
- Rotating clockwise and counterclockwise cost different amounts.
Where it's asked
- OpenAI · Agentic coding (beta) · Last asked Mar 2026An existing codebase and changes too large to write by hand; you work through them with an AI coding agent · An AI coding agent is expected
What it tests
dynamic programming · circular arrays
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.
