Code RoomLexicographically smallest Eulerian circuit
HardPrep Room Coding #1259

Lexicographically smallest Eulerian circuit

CodingAlgorithms & data structuresSenior–Staff~40 min

Given a directed multigraph on n nodes (0..n-1) with a list of directed edges, reconstruct the lexicographically smallest Eulerian circuit: a closed walk starting and ending at the same node that uses every edge exactly once. Return it as the list of visited nodes (length = number_of_edges + 1). If no Eulerian circuit exists (degree imbalance or the edges are not all connected into one circuit), return an empty list. Start the circuit from the smallest-numbered node that has an outgoing edge. Constraints: 1 <= n <= 1000; edges may include self-loops and parallel edges.

Implement
lex_eulerian_circuit(n: int, edges: list[list[int]]) → list[int]
Examples
in[3,[[0,1],[1,2],[2,0]]]out[0,1,2,0]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 40 min
InputExpectedGot
[3,[[0,1],[1,2],[2,0]]][0,1,2,0]not run yetsample