Google Software Engineering Intern, Fall 2019 – North America
1. Consider a binary tree of N vertices such that children of node k are 2*k and 2*k+1. Vertex 1 is the root of the tree and each node has an integer value associated with it.
Such a tree may be represented as an array of N integers by writing down values from consecutive nodes.
The tree can be represented as an array [-1, 7, 0, 7, -8].
A node is said to be at level x if the length of the shortest path between that node and root x-1. So, the root is at level 1, the children of root are at level 2, and so on.
Your task is to find the smallest level number x such that sum of all nodes at level x is maximal.
Examples: Given array A such that: A=-1, A=7, A=0, A=7, A=-8. The function should return 2.
Input : [-1, 7, 0, 7, -8] Output : 2
2. Imagine you have a special keyboard with all keys in a single row. The layout of characters on a keyboard is denoted by a string S1 of length 26. S1 is indexed from 0 to 25. Initially, your finger is at index 0. To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index i to index j is |j-i|, where || denotes absolute value.
Write a function solution(), that given a string S1 that describes the keyboard layout and a string S2, returns an integer denoting the time taken to type string S2.
S1 = abcdefghijklmnopqrstuvwxyz
S2 = cba
Input : S1 = abcdefghijklmnopqrstuvwxyz, S2 = cba Output : 4