PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/ZipHQ

Maximize non-adjacent sum on an N-ary tree

Last updated: Mar 29, 2026

Quick Overview

This question evaluates skills in tree-based dynamic programming, recursive traversal, and per-node state management for selecting non-adjacent nodes in an N-ary tree.

  • medium
  • ZipHQ
  • Coding & Algorithms
  • Software Engineer

Maximize non-adjacent sum on an N-ary tree

Company: ZipHQ

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an **N-ary tree** (each node may have 0..k children). Each node contains an integer value (can be 0 or positive). You want to select a set of nodes such that **no selected node is directly connected to another selected node** (i.e., you cannot select both a node and any of its children). Return the **maximum possible sum** of selected node values. ### Requirements - Implement a tree node structure yourself, e.g.: - `value: int` - `children: List[Node]` - Write a function `maxNonAdjacentTreeSum(root) -> int`. ### Input / Output - **Input:** `root` pointer/reference to the root node (or `null` for empty tree). - **Output:** an integer representing the maximum achievable sum. ### Example If the tree is: - Root 3 with children 2 and 3 - Node 2 has child 3 - Node 3 (child of root) has child 1 One optimal selection is nodes with values `3 (root) + 3 (grandchild under 2) + 1 = 7`, so return `7`. ### Constraints - Number of nodes `n` up to ~10^5. - Aim for **O(n)** time. - Extra space should be proportional to recursion depth or explicit stack size.

Quick Answer: This question evaluates skills in tree-based dynamic programming, recursive traversal, and per-node state management for selecting non-adjacent nodes in an N-ary tree.

Related Interview Questions

  • Find shortest path on infinite grid - ZipHQ (hard)
  • Find flush and straight groups in cards - ZipHQ (medium)
  • Compute maximum sum in a tree without adjacency - ZipHQ (Medium)
  • Find root IDs and paths - ZipHQ (Medium)
ZipHQ logo
ZipHQ
Dec 26, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
28
0

You are given an N-ary tree (each node may have 0..k children). Each node contains an integer value (can be 0 or positive).

You want to select a set of nodes such that no selected node is directly connected to another selected node (i.e., you cannot select both a node and any of its children). Return the maximum possible sum of selected node values.

Requirements

  • Implement a tree node structure yourself, e.g.:
    • value: int
    • children: List[Node]
  • Write a function maxNonAdjacentTreeSum(root) -> int .

Input / Output

  • Input: root pointer/reference to the root node (or null for empty tree).
  • Output: an integer representing the maximum achievable sum.

Example

If the tree is:

  • Root 3 with children 2 and 3
  • Node 2 has child 3
  • Node 3 (child of root) has child 1

One optimal selection is nodes with values 3 (root) + 3 (grandchild under 2) + 1 = 7, so return 7.

Constraints

  • Number of nodes n up to ~10^5.
  • Aim for O(n) time.
  • Extra space should be proportional to recursion depth or explicit stack size.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More ZipHQ•More Software Engineer•ZipHQ Software Engineer•ZipHQ Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.