35Redirect chains
35 Redirect chains
Intersection of Two Linked Lists is on Airbnb's LeetCode company tag lists, both the all-time list and the six-month list (2 sources). A prep site's write-up of a senior Airbnb onsite loop lists finding where two linked lists with cycles overlap (1 source), and a software engineer candidate reported being handed two linked lists, each of which may or may not contain a cycle (1 report). Other questions share the linked list without these details: LRU Cache is on the compiled LeetCode lists for OpenAI and Netflix and in a senior Netflix candidate's report (3 sources), and Design Authentication Manager, tagged as a doubly linked list question, is on OpenAI's lists (2 sources). This is practice in that shape, not a leaked question or LeetCode's version.
Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/35_redirect_chains -k level1. Earlier levels must keep passing.
The input, at every level. A link service follows redirects. Each redirect is a hop: an object with a url and
a next, the hop it sends you to. The final page's hop has next set to None. A chain is given by its first hop,
and None is the empty chain. solution.py defines Hop(url, next=None) for your own tests.
The tests build their chains from a class of their own with the same two attributes, and it counts every read of
.next. Each call may read .next at least 20 times for every distinct hop it can reach, far more than a
correct solution needs, and a call that goes past its allowance fails right there. So a walk that goes around a loop
forever fails fast instead of hanging. Do the same in any test you write: give every walk a limit.
Two hops can hold the same url and still be different hops, the way two campaigns can each create their own tracking
record for one address. A hop that two chains share is one object that both reach, and you compare hops with is.
sale = Hop("shop.example/sale")
nine = Hop("track.example/9", Hop("cdn.example/x", sale))
a = Hop("link.example/a", Hop("track.example/1", Hop("track.example/2", nine)))
b = Hop("link.example/b", nine)
Chain a runs through 6 hops, chain b through 4, and both reach track.example/9, then cdn.example/x, then the
page.
Level 1. One chain that ends at a page. Change it in place: move next pointers, never build new hops.
drop_hops(head, blocked)removes every hop whose url is in the setblocked, however many hops hold that url, and returns the first hop left, orNoneif none is left. Leaveblockedas it was. The hops it keeps are the same objects, in the same order. Onawithblocked = {"track.example/1", "track.example/2"}it returns the hoplink.example/a, which now points attrack.example/9.reverse_chain(head)reverses the chain and returns its new first hop, the one that was last. On a chainx.example/1,x.example/2,x.example/3it returns the hopx.example/3, which now points atx.example/2, andx.example/1now points atNone.- Both must work on a chain of 20,000 hops with Python's default recursion limit.
Level 2. Where two chains meet. Neither chain loops. first_shared(a, b) returns the first hop on chain a that
chain b also reaches, or None if they share no hop. Once two chains share a hop they share every hop after it, so
here the answer is the same either way round: above, first_shared(a, b) and first_shared(b, a) are both the hop
track.example/9. Two rules:
- Leave both chains exactly as you found them.
- Use O(1) extra memory: no set or list of hops. The tests measure the peak with
tracemallocon chains of 40,000 and 30,000 hops and allow 64 KiB.
Level 3. Chains that loop. A misconfigured hop can point back at an earlier one, and then the chain never reaches a page.
loop_start(head)returns the first hop of the loop, the hop on the loop that a walk fromheadreaches first, orNoneif the chain ends. O(1) extra memory, checked on a chain of 50,000 hops.first_shared(a, b)now takes chains that may or may not loop. The definition doesn't change: the first hop ona's walk thatb's walk also reaches. The two rules from level 2 still hold, and the memory check runs on a loop of 20,000 hops.
When both chains run into the same loop at different hops, every hop on the loop is reached by both, and no hop is
first for both. The definition settles it: the answer is the hop where a enters the loop, so first_shared(b, a)
is the hop where b enters. Say a loop runs cdn.example/0 to cdn.example/5 and back to cdn.example/0, chain a
is link.example/a, track.example/1, then cdn.example/1, and chain b is link.example/b, then cdn.example/4.
Then loop_start(a) and first_shared(a, b) are the hop cdn.example/1, and first_shared(b, a) is the hop
cdn.example/4. A chain that starts on the loop enters it at its own first hop. A loop shares nothing with a chain
that ends, and two different loops share nothing.
Things to get right: a blocked first hop, two blocked hops in a row, a chain too long for recursion, two hops with the same url, chains of different lengths, a chain that starts inside the other, a loop back to the very first hop, a hop that points at itself, and every way two chains can end: both at a page, one at a page and one in a loop, in two different loops, in one loop they enter at the same hop, and in one loop they enter at different hops.
Be ready to talk about what comes next: why the two pointers in loop_start must meet and why the second walk finds
the loop's first hop, how you'd merge two chains whose urls are sorted, what breaks when a chain is longer than
Python's recursion limit, how you'd copy a chain whose hops also point at a fallback hop, and why a limit on steps
belongs in any code that walks data someone else wrote.