Pick Promoted Videos in a Recommendation Tree Without Parent-Child Pairs

Last updated: November 29, 2026

Quick Overview

A binary tree of candidate videos has an expected engagement score at each node. Choose a set of nodes with maximum total score such that no chosen node is the direct parent of another chosen node.

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

0

0

0 solved


A binary tree of candidate videos has an expected engagement score at each node. Choose a set of nodes with maximum total score such that no chosen node is the direct parent of another chosen node.

A Google onsite tree dynamic programming question with a YouTube framing: promoting both a video and its direct follow-up hurts diversity. The interviewer looks for the two-state post-order DP rather than memoizing on grandchildren.

What the Interviewer Expects
  • Define two states per node: chosen and not chosen
  • Combine children's states correctly for each case
  • Compute everything in one post-order pass
  • Explain why greedy choices like taking every other level fail
  • State O(n) time and O(h) space
Key Topics to Cover
binary trees
tree dynamic programming
post-order traversal
independent set
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
  • Return which nodes were chosen, not just the score.
  • What if chosen nodes must also be at least distance 3 apart?
  • Generalize to an n-ary tree.
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: root with non-negative integer scores. Output: maximum sum of a subset of nodes containing no parent-child pair. Empty tree returns 0. Grandparent and grandchild may both be chosen....

High-Level Design

For each node return a pair (take, skip): the best total for its subtree if the node is chosen, and if it is not. If the node is chosen, neither child may be chosen, so `take = val + left.skip + rig...


Submit Your Answer
Markdown supported