Match a Linked Route Along a Downward Path in a Place Tree

Last updated: November 29, 2026

Quick Overview

Given the head of a singly linked list of place IDs and the root of a binary tree of places, return True if the list's values appear in order along some downward path (parent to child) in the tree.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given the head of a singly linked list of place IDs and the root of a binary tree of places, return True if the list's values appear in order along some downward path (parent to child) in the tree.

A Google onsite problem that mixes linked lists with tree recursion, in a Maps hierarchy framing (country, region, city). The interviewer checks that you separate 'match starting here' from 'try every start' and can state the worst-case cost.

What the Interviewer Expects
  • Write a helper that matches the list starting at a given tree node
  • Try every tree node as a starting point
  • Short-circuit as soon as a match is found
  • State the O(n * m) worst case and when it occurs
  • Mention a KMP-style speedup for long lists
Key Topics to Cover
linked list
binary tree
recursion
pattern matching
DFS
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Allow the path to go upward as well (any path).
  • Count how many tree nodes start a matching path.
  • Speed it up with KMP failure links on the list.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Inputs: list head (length m >= 1) and tree root (n nodes).
  • Output: True if some downward path, starting anywhere, spells the list.
  • Path must follow parent to child edges consecutively....
High-Level Design

Split the problem in two. match(list_node, tree_node) returns True if the list from list_node can be traced downward starting exactly at tree_node: if the list is exhausted, success; if the tree...


Submit Your Answer
Markdown supported