Part 1: Unit Conversion (LeetCode)
Question type: Graph / Tree Traversal
Problem description:
Given a series of conversion relationships between units, calculate the equivalent value of every connected unit relative to a starting unit.
If unit A converts to unit B with conversion factor x, and A has value v, then B's value is v * x.
Approach:
This is a very classic weighted-graph traversal question. Treat the conversion relationships as a weighted undirected graph, or a directed tree.
Start from the base unit and traverse with DFS or BFS. During traversal, propagate the accumulated conversion factor along each edge.
Time complexity: O(N), where N is the number of units.
Space complexity: O(N), for the graph and visited state.
Part 2: Backend Debugging and Code Review
Ticket Confirmation Debugging
Topics tested: Django / Backend Development / State Consistency / Redis
Background:
Fix the "confirm purchase" flow in a ticketing application. A ticket in the "held" state must become "confirmed", while keeping five things consistent: the database ticket state, section inventory counts, hold expiration state, Redis hold state, and the event's sold-out state.
Required behavior:
The ticket must currently be held.
The hold must not have expired.
The ticket must belong to the acting user.
On success: ticket.status becomes confirmed; ticket.hold_expires_at becomes null; section.held_count decreases by 1; section.sold_count increases by 1; delete the hold key in Redis; finally, check whether the event is sold out.
Error requirements:
Not held: 400, "only held tickets can be confirmed".
Expired hold: 400, "ticket hold has expired".
Ticket missing or not owned by the user: 404, "ticket not found".
Key debugging insight / pitfalls:
This question really tests consistency across the database, lifecycle, inventory, and Redis. The easiest bug to miss is that the Redis helper constructs the hold key but never actually deletes it. Also watch the order of state checks—for example, check whether the ticket exists before checking whether it has expired.
Discussion
Loading comments…