34Regions inside regions
34 Regions inside regions
Smallest Common Region, a tree question about regions that contain smaller regions, is on Airbnb's LeetCode company tag list, both the all-time list and the six-month list (2 sources). An Airbnb software engineer candidate reported a tree whose leaves hold numbers and whose every parent holds the sum of its children, with values that change and reads of any node in between (1 report). Other reports share the pattern without these details: two Netflix software engineer candidates describe their coding question as a depth-first search over a tree (2 reports), and two OpenAI candidates report counting the machines in a tree by sending messages (2 reports). A prep site also lists the largest sum of tree nodes with no two of them next to each other for an Airbnb senior loop (1 source). 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/34_region_tree -k level1. Earlier levels must keep passing.
The input, at every level. A travel site files its listings under regions, and regions sit inside bigger
regions. lists is a list of lists of region names. Each list is one region followed by every region directly
inside it, and a list may hold only its region. The lists come in any order. A region with nothing inside it may
have no list of its own and appear only inside another region's list. Names are distinct, each region is directly
inside at most one other, and exactly one region, the top, is inside none. Every region a method is asked about is
in the tree.
lists = [
["Japan", "Kanto", "Kansai"],
["Asia", "Japan", "Thailand"],
["Kanto", "Tokyo", "Yokohama"],
["Kansai", "Osaka", "Kyoto"],
["Thailand", "Bangkok", "Chiang Mai"],
["Tokyo", "Shibuya", "Shinjuku"],
]
Level 1. Write the class RegionTree(lists) and three methods.
top()returns the region no other region contains:"Asia"above, though Japan's list comes first.depth(region)is 0 for the top, 1 for the regions directly inside it, and so on:Japanis 1 andShibuyais 4.places_in(region)is how many regions lie insideregionat any depth, not counting itself:Japanholds 8 (Kanto, Kansai, Tokyo, Yokohama, Osaka, Kyoto, Shibuya, Shinjuku),Asiaholds 12, andShibuyaholds 0.
Level 2. Write smallest_common(a, b): the smallest region that contains both a and b, where a region
counts as containing itself. Above, Shibuya and Yokohama give "Kanto", Shinjuku and Kyoto give "Japan", Kyoto
and Bangkok give "Asia", Tokyo and Shibuya give "Tokyo", and Osaka with itself gives "Osaka".
Level 3. Listings, and a tree that can be very deep.
set_listings(region, count):regionnow hascountlistings filed directly under it, replacing whatever it had before. Every region starts with 0.listings(region): the listings filed underregionand under every region inside it.- Reads far outnumber changes, so a read must not walk the regions inside. The tests make 5,000 changes on a tree of 100,101 regions, read the top's total after each one, and allow a second for all of it.
- Every method must work on a chain of 50,000 regions, each directly inside the one before, with Python's default recursion limit, which your code must leave as it is.
After set_listings("Shibuya", 40), set_listings("Tokyo", 5) and set_listings("Kyoto", 12) on the tree above,
listings("Tokyo") is 45, listings("Japan") is 57 and listings("Thailand") is 0. Then set_listings("Shibuya", 30) makes Tokyo 35 and Asia 47.
Things to get right: the top when its list isn't first, regions that have no list of their own, a tree of one region, two regions at different depths, a region that contains the other, a count set twice, and a chain deeper than Python's recursion limit.
Be ready to talk about what comes next: what a change costs on a very deep tree and how to make both changes and reads O(log n), how you'd check that the lists really form one tree (a region inside two others, a loop, two tops), how you'd write the tree to a file and read it back, and how you'd count the regions if each one lived on its own machine and answered only messages.