Convert Leveled Rich-Text Tokens into Nested Lists

Quick Overview

Convert ordered rich-text tokens with positive depth levels into the specified nested-list representation while preserving text order. The exercise probes stack invariants, multi-level jumps, returns to ancestors, empty input, exact node creation, and complexity in both tokens and nesting.

Convert Leveled Rich-Text Tokens into Nested Lists

Company: Figma

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Convert Leveled Rich-Text Tokens into Nested Lists ### Problem Implement `read_rich_text(tokens)`. Each token is a pair `(level, text)` in document order, where `level` is a positive integer. Return a nested list that represents the levels. The outer result is the container for level 1. Before appending a token's text: - If its level is shallower than the current level, return to the existing ancestor container. - If its level is deeper, append one new child list for every missing level and descend through that chain. - Append the text to the container for its level. The input is guaranteed to be empty or to begin at level 1. Text values remain strings in the output. ### Examples ```text tokens = [(1, "food"), (4, "banana")] result = ["food", [[["banana"]]]] ``` ```text tokens = [(1, "a"), (2, "b"), (2, "c"), (1, "d")] result = ["a", ["b", "c"], "d"] ``` ```text tokens = [] result = [] ``` ### Requirements - Preserve token order. - Create exactly one list node per level transition that must be represented. - Do not mutate the input. - Explain the time and auxiliary-space complexity in terms of the number of tokens and created nesting levels. ```hint Trace level changes Work through consecutive tokens whose levels rise by three, stay equal, and then fall; note which existing output container receives each text value. ``` ### Discussion Prompts 1. What invariant connects stack length to the current token level? 2. How does the algorithm handle a jump directly from level 1 to level 4? 3. What validation would you add if the first token or a later level could be invalid?

Quick Answer: Convert ordered rich-text tokens with positive depth levels into the specified nested-list representation while preserving text order. The exercise probes stack invariants, multi-level jumps, returns to ancestors, empty input, exact node creation, and complexity in both tokens and nesting.

|Home/Coding & Algorithms/Figma
Figma logo
Figma
Jul 21, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
3
0

Convert Leveled Rich-Text Tokens into Nested Lists

Problem

Implement read_rich_text(tokens). Each token is a pair (level, text) in document order, where level is a positive integer. Return a nested list that represents the levels.

The outer result is the container for level 1. Before appending a token's text:

  • If its level is shallower than the current level, return to the existing ancestor container.
  • If its level is deeper, append one new child list for every missing level and descend through that chain.
  • Append the text to the container for its level.

The input is guaranteed to be empty or to begin at level 1. Text values remain strings in the output.

Examples

tokens = [(1, "food"), (4, "banana")]
result = ["food", [[["banana"]]]]
tokens = [(1, "a"), (2, "b"), (2, "c"), (1, "d")]
result = ["a", ["b", "c"], "d"]
tokens = []
result = []

Requirements

  • Preserve token order.
  • Create exactly one list node per level transition that must be represented.
  • Do not mutate the input.
  • Explain the time and auxiliary-space complexity in terms of the number of tokens and created nesting levels.

Discussion Prompts

  1. What invariant connects stack length to the current token level?
  2. How does the algorithm handle a jump directly from level 1 to level 4?
  3. What validation would you add if the first token or a later level could be invalid?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...