FastPrepInsert and Delete in a Trinary Tree
Problems/Insert and Delete in a Trinary Tree

Insert and Delete in a Trinary Tree

Zillow logoZillow● MediumFULLTIMENEW GRADINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported many times as part of a Zillow take-home coding test from about 2012 to 2016, for intern, new grad and experienced software roles. The task was to write insert and delete for a tree with three children per node, split by whether a value is less than, equal to or greater than the node.

The reports do not say which equal node a delete removes, how to delete a node that has both a left and a right child, or what happens when the value is missing. Here, one node of the equal chain goes away, the smallest node on the right side takes the deleted node's place, and a missing value changes nothing. The reports give no size limits, so we allow up to 500 operations. You get the calls as two lists and return the final tree in preorder instead of writing a class.

Our version is about 85% the same as the reported question.

Given two arrays operations and values, apply each operation in order to a trinary tree that starts empty, and return the values of the final tree in preorder.

A trinary tree is like a binary search tree, but each node has three children:

  • The left child holds values less than the node.
  • The middle child holds values equal to the node.
  • The right child holds values greater than the node.

Operation i is operations[i] applied to the value values[i]:

  • "insert": add the value as a new leaf. Start at the root and go left, middle or right by comparing the value with each node, until you reach an empty child spot.
  • "delete": remove one node with the value. If no node has the value, nothing changes. Otherwise, take the node with the value that is closest to the root:
    • If it has a middle child, remove one node from its chain of equal values. Nothing else in the tree changes.
    • Otherwise, if it has at most one child, that child (or nothing) takes its place.
    • Otherwise it has both a left and a right child. The smallest node in its right subtree takes its place and brings its own middle chain with it. That smallest node's right child takes the spot the smallest node left.

Preorder visits a node, then its left subtree, then its middle subtree, then its right subtree. Return an empty list if the final tree is empty.

Function

trinaryTree(operations: String[], values: int[]) → List<Integer>

Examples

Example 1

operations = ["insert","insert","insert","insert","insert","insert","insert"]values = [5,4,9,5,7,2,2]return = [5,4,2,2,5,9,7]

The root is 5. Its left child is 4, its middle child is the second 5, and its right child is 9. Node 4 has left child 2, whose middle child is the second 2. Node 9 has left child 7. Preorder gives [5,4,2,2,5,9,7].

Example 2

operations = ["insert","insert","insert","insert","insert","insert","insert","delete","delete","delete","delete"]values = [5,4,9,5,7,2,2,5,5,2,8]return = [7,4,2,9]

The inserts build the tree from Example 1. The first delete of 5 removes the middle 5. Now the root 5 has only a left child 4 and a right child 9, so the second delete of 5 moves 7, the smallest node on the right side, to the root. Deleting 2 removes the middle 2, and deleting 8 changes nothing.

Example 3

operations = ["insert","insert","insert","insert","insert","insert","insert","delete"]values = [10,5,20,15,15,17,25,10]return = [15,5,15,20,17,25]

The root 10 has no middle child and has both a left and a right child. The smallest node in its right subtree is the first 15. It becomes the root and keeps its middle 15, and its right child 17 becomes the left child of 20.

Example 4

operations = ["insert","delete","delete","insert"]values = [3,3,3,-1]return = [-1]

After the first delete the tree is empty, so the second delete of 3 changes nothing. Then -1 is inserted as the only node.

Constraints

  • 1 <= operations.length <= 500
  • values.length == operations.length
  • Each operations[i] is "insert" or "delete".
  • -10^5 <= values[i] <= 10^5

More Zillow interview questions

  • Build a Hash TableOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
  • First Unique Character in a StringOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
  • Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
  • First N Fibonacci NumbersOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2021
  • Convert a String to a LongOA · Seen Jan 2018
  • Find the Four Missing Numbers in a FileOA · PHONE SCREEN · Seen Feb 2014
  • Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
  • Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Sep 2019
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016

Similar questions at other companies

  • Reconstruct the Root StreamArcesium · OAArcesium interview questions
  • Battle with Upper Moon 6Rubrik · OARubrik interview questions
  • File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • RNG-Based Tree Document CodecBaseten · PHONE SCREENBaseten interview questions
  • Workspace File System With Folders and TablesClay · PHONE SCREEN
  • Nested Todo ListNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
  • Render a Comment HierarchyAmazon · ONSITE INTERVIEW
  • Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
  • Inherited Role PermissionsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
All Zillow interview questionsSee Zillow hiring insights
Saved
class Solution {
    public List<Integer> trinaryTree(String[] operations, int[] values) {
        // Write your code here.
    }
}
Checking account…