Most Frequent Category Along Any Path in a Browse Graph
Last updated: November 29, 2026
Quick Overview
Each page in a browse graph has a category letter, and directed links connect pages. Return the largest count of a single category along any path, or -1 if the graph has a cycle.
Amazon
November 29, 20260
0
0 solved
Each page in a browse graph has a category letter, and directed links connect pages. Return the largest count of a single category along any path, or -1 if the graph has a cycle.
Inspired by Amazon's browse node hierarchy, this combines topological sort with DP over a 26-letter state. The interviewer checks that you carry per-letter maxima along edges, handle cycle detection in the same pass, and keep complexity at O(26(n + m)).
What the Interviewer Expects
- Recognize DP over a DAG needs topological order
- Keep count[v][c] = max occurrences of letter c on a path ending at v
- Add the node's own letter when it is popped, then push maxima to children
- Detect cycles by counting popped nodes
- O(26 (n + m)) time and O(26 n) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the path itself?
- What if categories are arbitrary strings instead of 26 letters?
- How would you handle a graph that is too large for memory?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: string categories (one lowercase letter per page) and directed edges. A path is any sequence following edges. Output: the max over all paths and letters of how many pages on the path have tha...
High-Level Design
Let count[v][c] be the most pages with letter c on any path ending at v. Then count[v][c] = max over parents u of count[u][c], plus 1 if v's letter is c.
Process pages in Kahn order. When u is po...