Magicsheet logo

Tree Node

Medium
89.4%
Updated 6/1/2025

Tree Node

What is this problem about?

The "Tree Node coding problem" is a SQL challenge that requires you to classify nodes in a tree structure. You are given a table Tree with two columns: id and p_id (parent ID). Your task is to categorize each node as one of three types: "Root" (if the node has no parent), "Inner" (if the node has both a parent and at least one child), and "Leaf" (if the node has a parent but no children).

Why is this asked in interviews?

This "Tree Node interview question" is a fundamental test of SQL logic and conditional statements. Companies like Apple and Twitter use it to see if you can handle hierarchical data in a relational database. It evaluates your ability to use CASE statements and subqueries (or LEFT JOINs) to check for the existence of relationships (parents and children) efficiently.

Algorithmic pattern used

The "Database interview pattern" for this problem involves checking two conditions for each node.

  1. p_id IS NULL -> Root.
  2. id exists in the p_id column of the table -> It's a parent, so it's "Inner" (if it also has a parent) or "Root".
  3. Otherwise -> Leaf. A CASE statement is the most readable way to implement this. You can use an IN clause with a subquery to check if a node ID appears in the parent ID column.

Example explanation

Tree Table:

  • 1, null
  • 2, 1
  • 3, 1
  • 4, 2
  • 5, 2 Logic:
  • Node 1: p_id is null. Type: "Root".
  • Node 2: p_id is 1, and 2 is a parent of 4 and 5. Type: "Inner".
  • Nodes 3, 4, 5: have parents but are not parents of anyone. Type: "Leaf".

Common mistakes candidates make

One common mistake in the "Tree Node coding problem" is incorrectly identifying the root when there's only one node in the tree (it should still be "Root"). Another error is the order of conditions in the CASE statement; for example, checking if a node is a leaf before checking if it's the root can lead to wrong results. Also, when using IN (subquery), be careful if the subquery returns NULL values, as this can affect the IN logic in some SQL dialects.

Interview preparation tip

For the "Database interview pattern," practice using CASE statements to create new categorical columns based on existing data. Also, get comfortable with self-joins and EXISTS clauses, which are alternative ways to solve hierarchical problems without using nested IN subqueries.

Similar Questions