Quick Overview

Classify each tree node as Root, Inner, or Leaf in PostgreSQL using its parent ID and whether it has children, returning one classification per node.

Classify Tree Nodes in SQL

Company: Oracle

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

Write a PostgreSQL query that classifies every node in a tree as `Root`, `Inner`, or `Leaf`. ### Table `Tree` contains one row per node: | Column | Type | Meaning | | --- | --- | --- | | `id` | integer | Unique, non-null node identifier | | `pid` | integer, nullable | Parent node identifier, or `NULL` when the node has no parent | The input represents a nonempty tree. Each non-null `pid` refers to the `id` of its parent node. ### Classification rules - `Root`: the node has no parent, so its `pid` is `NULL`. - `Inner`: the node has a parent and at least one child. A child is a row whose `pid` equals this node's `id`. - `Leaf`: the node has a parent and no children. A root is classified as `Root` regardless of whether it has children, including when it is the tree's only node. ### Required output Return exactly one row per input node, with columns `id` and `type`. The `type` value must be exactly `Root`, `Inner`, or `Leaf` according to the rules above. Result rows may appear in any order. ### Example Input `Tree`: | id | pid | | --- | --- | | 1 | NULL | | 2 | 1 | | 3 | 1 | | 4 | 2 | Expected result, shown in identifier order only for readability: | id | type | | --- | --- | | 1 | Root | | 2 | Inner | | 3 | Leaf | | 4 | Leaf |

Overview: Classify each tree node as Root, Inner, or Leaf in PostgreSQL using its parent ID and whether it has children, returning one classification per node.

Read the full Oracle Software Engineer interview experience this question came from

Write a PostgreSQL query that classifies every node in a tree as `Root`, `Inner`, or `Leaf`. ### Table `Tree` contains one row per node: | Column | Type | Meaning | | --- | --- | --- | | `id` | integer | Unique, non-null node identifier | | `pid` | integer, nullable | Parent node identifier, or `NULL` when the node has no parent | The input represents a nonempty tree. Each non-null `pid` refers to the `id` of its parent node. ### Classification rules - `Root`: the node has no parent, so its `pid` is `NULL`. - `Inner`: the node has a parent and at least one child. A child is a row whose `pid` equals this node's `id`. - `Leaf`: the node has a parent and no children. A root is classified as `Root` regardless of whether it has children, including when it is the tree's only node. ### Required output Return exactly one row per input node, with columns `id` and `type`. The `type` value must be exactly `Root`, `Inner`, or `Leaf` according to the rules above. Result rows may appear in any order.

Tables

tree(id INTEGER, pid INTEGER)

Hints

  1. Return one classification row for every input node.
  2. A root remains Root whether or not it has children.

Loading coding console...