FastPrepNested Todo List

Nested Todo List

Notion logoNotion● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Process operations against an initially empty nested todo list. Items form an ordered forest.

  • ["ADD", id, parentId, text] adds a pending item. A parentId of - makes it a root; otherwise it becomes the newest child of that parent.
  • ["TOGGLE", id] switches that item between PENDING and DONE. Toggling one item does not change descendants or ancestors.
  • ["GET", id] returns id|parentId|status|text.
  • ["RENDER"] returns every item in preorder. Each line is depth|id|status|text, roots and siblings retain insertion order, and lines are joined by a newline.

Return the values produced by GET and RENDER operations in order.

Function

runNestedTodoList(operations: String[][]) → String[]

Examples

Example 1

operations = [["ADD","1","-","Plan"],["ADD","2","1","Draft"],["ADD","3","1","Review"],["TOGGLE","2"],["GET","2"],["RENDER"]]return = ["2|1|DONE|Draft","0|1|PENDING|Plan\n1|2|DONE|Draft\n1|3|PENDING|Review"]

The two children retain insertion order. Toggling item 2 changes only that item.

Constraints

  • 1 <= operations.length <= 20000.
  • IDs are unique non-empty strings containing neither | nor whitespace.
  • Every non-root parent exists before its child is added.
  • Every GET and TOGGLE references an existing item.
  • Text is non-empty and contains neither | nor a newline.
  • The total number of rendered item lines is at most 200000.

More Notion problems

See Notion hiring insights
public String[] runNestedTodoList(String[][] operations) {
    // Write your code here.
}
operations[["ADD","1","-","Plan"],["ADD","2","1","Draft"],["ADD","3","1","Review"],["TOGGLE","2"],["GET","2"],["RENDER"]]
expected["2|1|DONE|Draft", "0|1|PENDING|Plan\n1|2|DONE|Draft\n1|3|PENDING|Review"]
Checking account…