PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Others

Answer k-step ancestor queries in a rooted tree

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency with tree data structures and ancestor-query algorithms, focusing on reasoning about parent arrays, upward traversal, and performance under large n and q constraints.

  • medium
  • Others
  • Coding & Algorithms
  • Software Engineer

Answer k-step ancestor queries in a rooted tree

Company: Others

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given a rooted tree encoded as a parent array `par[1..n]` where: - Nodes are labeled `1..n`. - `par[i]` is the parent of node `i`. - Exactly one node has `par[i] = -1` (the root). You must answer `q` queries. Each query provides a starting node `u` and an integer `k`. Starting from `u`, apply the following operation **exactly `k` times** (or until you cannot move further): - If the current node has a parent, move to its parent. - If the current node is the root (parent is `-1`), you stay at the root for all remaining steps. For each query, output the final node label after performing the moves. ### Input format - `n` - array `par[1..n]` - `q` - `q` lines: `u k` ### Output format - For each query, one line with the final node label. ### Constraints (typical interview scale) - `1 <= n, q <= 2 * 10^5` - `0 <= k <= 10^9` - `par[i] = -1` or `1 <= par[i] <= n` ### Example If `par = [-1, 1, 1, 2, 2, 2, 2]` (meaning `par[1]=-1, par[2]=1, ...`): - Query `(u=7, k=2)` goes `7 -> 2 -> 1`, output `1`. - Query `(u=4, k=10)` ends at root `1`, output `1`.

Quick Answer: This question evaluates proficiency with tree data structures and ancestor-query algorithms, focusing on reasoning about parent arrays, upward traversal, and performance under large n and q constraints.

Related Interview Questions

  • Return the largest file size in a directory - Others (easy)
  • Answer ancestor-walk queries on a rooted tree - Others (easy)
Others logo
Others
Jan 20, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
0
0
Loading...

You are given a rooted tree encoded as a parent array par[1..n] where:

  • Nodes are labeled 1..n .
  • par[i] is the parent of node i .
  • Exactly one node has par[i] = -1 (the root).

You must answer q queries. Each query provides a starting node u and an integer k.

Starting from u, apply the following operation exactly k times (or until you cannot move further):

  • If the current node has a parent, move to its parent.
  • If the current node is the root (parent is -1 ), you stay at the root for all remaining steps.

For each query, output the final node label after performing the moves.

Input format

  • n
  • array par[1..n]
  • q
  • q lines: u k

Output format

  • For each query, one line with the final node label.

Constraints (typical interview scale)

  • 1 <= n, q <= 2 * 10^5
  • 0 <= k <= 10^9
  • par[i] = -1 or 1 <= par[i] <= n

Example

If par = [-1, 1, 1, 2, 2, 2, 2] (meaning par[1]=-1, par[2]=1, ...):

  • Query (u=7, k=2) goes 7 -> 2 -> 1 , output 1 .
  • Query (u=4, k=10) ends at root 1 , output 1 .

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

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

Master your tech interviews with 8,500+ 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.