The test was for 1 hour. It consisted of 2 coding questions
Question 1
You were given a Binary Tree (not necessarily a Binary Search Tree) to play with, say T. T had some special properties
Each internal node in T had exactly 2 children Each internal node in T was represented by an uppercase English alphabet (A-Z) Each leaf node in T was represented by a lowercase English alphabet (a-z) You were told remember T as long as you could. Hence, you memorised the string formed by traversing T in post-order. You used something similar to the pseudocode below
toPostOrderString (node)
if node is leaf
return node.value
else
T = “”
T = T + toPostOrderString(node.left)
T = T + toPostOrderString(node.right)
T = T + node.value
return T
Now, time has come to use that string again. The Eye has contacted you. Yes, the secret organisation mentioned in “Now you see me” ( don’t tell anyone they are real !! )
You remember the string you memorized back then. You must reconstruct the binary tree T. You are also given a string A. All the characters of A are uppercase English alphabets. Let us assume that T has L leaves. Then, there will be exactly L paths from the root to the leaves – 1 unique path to each leaf.
You have to tell The Eye the number of paths out of L, on which, A exists as a sub-sequence. Look at the explanation for the Sample Case 1 for clarity.
You have to implement the method explodePaths in the code. explodePaths is passed the following parameters, respectively
N, the number of nodes in T
S, the string representation of the post-order traversal of T.
Of course, the length of S will be equal to N.
K, the length of the string A
A, the string you must find in the paths from the root of T, to the leaves in T.
Note
It is not necessary that T is balanced. But, each internal node always has exactly 2 children. It is possible that both those children are internal nodes also. It is possible that only one of those children is an internal node. For the given string S, because of the constraint that each internal node has exactly 2 children, you will always be able to determine the tree T, uniquely. It is not necessary that all characters in T are unique. There may be several nodes with the same value. In this problem statement, by sub-sequence we mean not necessarily contiguous. This is different from a sub-string. Do not print the answer in explodePaths. Just return the value. The code-template interviewstreet provides does the input and output itself.
Consider the following tree
A
/ \
t B
/ \
/ \
B A
/ \ / \
x y a b
This tree is given in Sample Case 1 as
N = 9
S = “txyBabABA”
K = 2
A = “AA”
Now, there are 5 leaf nodes, and hence, 5 paths from the root to leaves : 1 for each leaf.
– A-t
– A-B-B-x
– A-B-B-y
– A-B-A-a
– A-B-A-b
Out of these 5 paths, you have to find the number of paths, on which “AA” exists as a sub-sequence. Of course, there are only 2 such paths
– A-B-A-a – A-B-A-b Hence the expected answer is 2.
In the same T above
The answer for A = “BB”, is 2
The answer for A = “BA”, is 2
The answer for A = “AB”, is 4
The answer for K = 1 and A = “A”, is 5
The answer for K = 1 and A = “B”, is 4
Constraints
N ≤ 10000 K ≤ 100.
The expected time complexity of the algorithm is O(N).
Question 2
It is the IPL auction time.
Royal Challengers team management had prepared a set of X top quality players among the marquee list of the auction. Given the monetary constraints they would not be able to bid for all of them. Though the natural way of handling this was based on cricketing abilities of the players the management was put in a fix because of a promotional campaign run by Royal Challenege Mineral Water – the main sponsors of the team jersey.
As per the campaign the top Y consumers of Royal Challenge Mineral Water were selected as an esteemed set of fans for their team and each one of them got a chance to give their preferred set of players among the X chosen by the team management. What has put the team management in a fix is that they need to bid for at least one player in each of the fans preference list. Though the bidding strategy changes a lot at real time, the team management wants to find minimum number of players among X that they should bid, such that each fan is satisfied (i.e., at least one player from their preference list is picked).
Help the team management with this and get a chance to interview with them for a role of computer based sports analyst. To stop you from using your bias, the names of players are replaced by identifiers.
You have to finish the implementation of the function minimumStars. The input to minimumStars comprises of
numberOfPlayers – the number of players that the team management short listed
numberOfFans – the number of fans whose preference must be met
fansPreferences – an array of numberOfFans strings with numberOfPlayers characters in each string.
The jth character in the ith string is 1 if the jth player is in the ith player’s preference list, or 0 otherwise.
The function must return minimum number of players that must be selected such that for each fan, at least one of his preferred player is selected. It is possible that some fan has sneaked in a preference where he has not preferred any player. It is not possible to satisfy this fan. If it is not possible to satisfy any one of the fans, the function should return -1.
Constraints
numberOfPlayers <= 10
numberOfFans <= 100
Your code is considered correct only if you pass all Test Cases
Sample Case 1
Input
numberOfPlayers = 3
numberOfFans = 3
fansPreferences = { “110”, “001”, “011” }
Output
2
Assuming everything is 1-indexed, selecting player 1 and player 2 would appease fan 1 and fan 3, but none of the favourites of fan 2 would have been present.
Sample Case 2
Input
numberOfPlayers = 5
numberOfFans = 3
fansPreferences = { “10000”, “00011”, “01100” }
Output
3
The expected complexity for the solution is O( (numberOfFans) * 2^(numberOfPlayers) ).