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
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
topological sort
DP on DAG
cycle detection
frequency counting
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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported