Facebook | Phone screen | Vertical Level Sum of a Tree & Word Break

Got 2 questions. Can do 1st one but not 2nd, didn't pass in the end. Hopefully helpful to some folks.

Qn 1: Vertical level sum of a tree
Given a tree. Root is level 0. Level is defined as left child is root level - 1, right child is root level + 1. Return a map of level number and its sum.

	 3
    / \
   2   4
  / \
-1   7

Ans should be {-2:-1, -1:2, 0:10, 1:4}
7 is level 0 because it is left then right child of root, so its level is 0-1+1 == 0
Another example

Just do dfs

Qn 2: Word Break
Similar to LC 139. Word Break but not exactly. Given a non-empty string s, and map containing non-empty string as keys and count as values, determine if s can be segmented into a sequence of one or more words.

s = "abcd", map = {"abc":1, "ab":1, "cd":1} return true because s can be broken into "ab" and "cd"
s = "aaab", map = {"a":2, "b":2} return false as not enough a

Comments (10)