FastPrepTree-Dependent Knapsack
Problems/Tree-Dependent Knapsack

Tree-Dependent Knapsack

Google logoGoogle● HardINTERNNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was a PhD intern interview. The author says this is a knapsack problem whose dependencies form a tree, and picking an item requires picking its parent. The cost, value, and capacity interface, the input limits, and the examples were not kept. Our version is about 88% the same as the reported question.

You are given n items arranged as one rooted dependency tree. Item i has cost cost[i], value value[i], and parent parent[i]. The root has parent -1.

You may select any set of items whose total cost is at most capacity. Selecting a non-root item requires selecting its parent, which also recursively requires every ancestor through the root.

Return the maximum total value of a valid selected set. Selecting no items is allowed.

Function

maximizeDependentValue(parent: int[], cost: int[], value: int[], capacity: int) → int

Examples

Example 1

parent = [-1,0,0,1]cost = [2,2,3,2]value = [3,4,5,6]capacity = 6return = 13

Select items 0, 1, and 3. Their total cost is 6, their value is 13, and every selected item's parent is present.

Example 2

parent = [-1,0,0]cost = [5,1,1]value = [2,100,100]capacity = 2return = 0

The root does not fit, so neither child can be selected.

Example 3

parent = [-1,0,0,2]cost = [1,3,2,2]value = [1,8,4,10]capacity = 5return = 15

Select the root, item 2, and item 3 for total cost 5 and value 15.

Constraints

  • 1 <= n == parent.length == cost.length == value.length <= 200.
  • Exactly one index has parent -1; all other parent indices define one rooted tree.
  • 0 <= cost[i] <= capacity <= 2000.
  • 0 <= value[i] <= 10^6.
  • The answer fits in a signed 32-bit integer.

More Google Phone Screen questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
  • Lowest Common Manager for Employee GroupsAmazon · PHONE SCREEN
  • Longest Increasing Path in a MatrixNew Relic · PHONE SCREEN · ONSITE INTERVIEW
  • File Encryption Tree OptimizationDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
  • Subtree Access OverridesPinterest · PHONE SCREENPinterest interview questions
  • Maximum Path Sum Between Marked NodesDoorDash · PHONE SCREENDoorDash interview questions
  • House Robber IIIOkta · PHONE SCREENOkta interview questions
  • House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • House Robber IIIVisa · PHONE SCREENVisa interview questions
  • Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions

Interview reports that mention this question

  • Google SWE Intern Onsite Questions: 16 Experiences (Part 2 of 3)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int maximizeDependentValue(int[] parent, int[] cost, int[] value, int capacity) {
        // Write your code here.
    }
}
Checking account…