FastPrepDecrypt a String from Binary Tree Columns

Decrypt a String from Binary Tree Columns

Bloomberg LP logoBloomberg LP● MediumNEW GRADFULLTIMEPHONE SCREEN

Updated

Problem statement

Given a binary tree stored in parallel arrays, return the decrypted string: the tree's letters read column by column. Every node holds one lowercase English letter.

  • values[i] is node i's one-letter string.
  • leftIndices[i] and rightIndices[i] are the indices of its left and right children, or -1 when that child is missing.
  • rootIndex is the root, or -1 for an empty tree.

The root is in column 0. A left child is one column left of its parent, and a right child is one column right. Read the columns from leftmost to rightmost, and read each column from top to bottom. If two nodes share both row and column, take them in breadth-first order, which reads each row from left to right.

Function

decryptTreeColumns(values: String[], leftIndices: int[], rightIndices: int[], rootIndex: int) → String

Examples

Example 1

values = ["i","r","e","p","c"]leftIndices = [1,3,4,-1,-1]rightIndices = [2,-1,-1,-1,-1]rootIndex = 0return = "price"

The columns are [p], [r], [i,c], and [e]. Node c is the left child of e, so it shares the root's column.

Example 2

values = ["a","b","c","d","e","f","g"]leftIndices = [1,3,5,-1,-1,-1,-1]rightIndices = [2,4,6,-1,-1,-1,-1]rootIndex = 0return = "dbaefcg"

Nodes e and f share a row and a column. A breadth-first walk reaches e first, so e comes before f.

Example 3

values = []leftIndices = []rightIndices = []rootIndex = -1return = ""

An empty tree decrypts to the empty string.

Constraints

  • 0 <= values.length <= 100000.
  • The three arrays have equal length.
  • Every values[i] contains exactly one lowercase English letter.
  • Every child index is -1 or a valid array index.
  • When nonempty, the arrays describe one valid rooted binary tree and every node is reachable exactly once from rootIndex.
  • rootIndex == -1 exactly when the arrays are empty.

More Bloomberg LP Phone Screen questions

Similar questions at other companies

Interview reports that mention this question

Bloomberg LP interview experiences

All Bloomberg LP interview questionsSee Bloomberg LP hiring insights
Saved
class Solution {
    public String decryptTreeColumns(String[] values, int[] leftIndices, int[] rightIndices, int rootIndex) {
        // Write your code here.
    }

}
Checking account…