There were two algorithmic coding rounds of 90 Minutes each for placements at Directi.
1st Coding Round. 2 Problems . Qualified all students who were able to solve atleast one question. Mostly DFS, Dynamic Programming questions. Easy category problems.
2nd Coding Round 3 Problems . Interviews were taken based on the rank obtained by candidates in the coding rounds.
Problem 1 : There are two arrays A and B of size N each. You can take 1st element from either 1st array or 2nd array , 2nd element from 1st array A or array B and so on. You have to select in such a way that the total cost of all the N products is minimized.
Hint : Dynamic Programming
Problem 2 : You are given a rooted Tree of N ( 10^5 nodes ). There would be M updates (10^5) to the tree which are of the form
Add x y – Add y to node x .
AddUp x y – Add y to x , parent of x , parent of parent of x uptill Root.
After that there will be Q queries ( 10^5) queries where you will either be asked to give the value of a node or the sum of subtree rooted at that node.
Hint : Lazy Update as queries have no update value query.
Problem 3 : You are given a series of words. Let us say k words. You can map characters { A…..Z } to characters { A…..Z } in such a way that the words after encoding with the given mapping gives new words which are anagrams of the original words. Now what was asked is to find out the number of different ways in which the mapping can be done. It was also given that the total characters in all the words are always less than 16.
Eg. — work hard
The number of different mappings are 3! * 3! .
As character r is present in both the words along with the rest of the characters distinct with the same character count, it can only map to itself. Rest all characters i.e. w, o and k in first word and h, a, and d can map to each other as they have the same word count. So permuting them will give an anagram of the given word. So answer is 3! * 3! .
Hint : BitMask
Solution ( Difficulty – Medium )
First of all we have to create a Bipartite graph with the first partite showing the characters which are to be mapped and the second partite showing the encoding of the characters. The edges in this bipartite graph is constructed by first looping on each of the given word and making an edge if the characters are present in a word ( as encoded word should be an anagram of the original word ) and have the same count. If the character pair are not present in a single word then there will never be any edge between the character pairs.
Now the problem simply reduces to finding the number of maximum matching in the given bipartite graph which can be solved using BitMask. You can read the BitMask basics from my previous post ( Link ) .
Interview – 1 ( Skype Interview )
Problem 1 : You are given k words and their respective positions in the paragraphs as shown below :
W1 : 1 , 10 , 18 , 21 , 25
W2 : 3 , 11 , 20
W3 : 10 , 22 , 30 , 45
W4 : 15 , 23 , 40
You have to give the minimum sized window which contains all the words i.e. W1 , W2 , W3 and W4.
Solution 1 : Create a combined data structure of the word id and its position and sort it by the position. Now, you can keep a separate array of size k where you keep the latest position of the words in the array. Now loop on all the sorted array and whenever all the words latest position is known then you can find the different of the largest and the smallest value and return the minimum value thus obtained. Complexity : O ( N * log (N) ) + O ( N * k )
Solution 2 : In the previous solution instead of checking the whole k sized array to find the minimum and maximum and then giving the minimum window size we can keep a separate Set ( AVL Tree ) to find the minimum and maximum in O ( log k ) time.
Solution 3 : Further improvement can be made by keeping a linked list implemented in an array based on the position of the words. In this method the factor of O ( N * log ( k ) ) reduces to O ( N ) as the operation of finding the maximum and minimum can now be done in O ( 1 ) .
Problem 2 : You are given a complete Rooted Binary Tree in which each edge has some weight. A term Sum ( X ) is defined as the sum of all path lengths from node X to all the other nodes of the nodes. Now what is asked is to find the total sum for all the values of Sum ( X ). ( Hint : A simple DFS gives the result as each edge is counted the product of number of times of the nodes on either side of the edge. ) What was asked is to complete the recursive equations in terms of the child and parent nodes i.e.
count ( X ) : Number of nodes in the subtree rooted at X.
// Simply adding the children values and 1 for itself gives the result .
childSum ( X ) : The factor of Sum ( X ) which will be coming from its children nodes.
// childSum ( 2 * X ) and child ( 2 * X + 1) plus the number of times the edge from node X to (2 * X) will be added and similarly edge from node X to (2 * X + 1) will be added.
ancestor ( X ) : The factor in Sum ( X ) which is coming from its ancestor nodes.
// ancestor ( X ) includes all the nodes other than the nodes in the subtree rooted at node X. Hence the contribution of the sibling also has to be taken into consideration.
Interview – 2 ( Skype Interview )
Question 1 : You are two arrays of size N each. The first array contains only 1’s and 0’s. The second array contains values in the integer range. You can only flip the adjacent values in the first array from (0,1) -> (1,0) and vice versa ( and nothing else ) . You can apply this operation as many number of times as possible. Now what was asked to do is do this operation on the first array as many times such that the final sum of values in the second array corresponding to the 1’s in the first array should be maximum .
Hint : All possible arrangements of 1’s and 0’s are possible. If the number of set bits are k then take maximum k values from the second array.
Question 2 : You are given two strings A and B which are anagram of each other. You can perform only one type of operation on the first string i.e. choose any character and bring it to the first index thus shifting all characters in between one step right. Example : String A : abcdefgh and doing the operation on e gives us the new string as eabcdfgh. Now what was asked to do is to transform String A to String B in minimum number of steps possible.
Input :
abcd
cbad
Output :
2
Explanation : abcd -> bacd -> cbad
Hint : Check what happens to the characters that remain unmoved.
Question 3 : You are given a Simple graph with two people located at nodes P and Q of the graph. Now P wants to go to node P1 and Q wants to go to node Q1. In each time unit either person at P can move to its neighboring nodes or Q can move to any of its neighboring nodes but not both. You have to tell there is a path in the movement from P to P1 and Q to Q1 where the minimum distance between nodes is always greater than a fixed value D at any point of time in the path.
Hint : Think in terms of BFS on the pair of initial nodes.
Interview – 3 ( Telephonic Interview – No Coding )
First there was a general questionnaire about my previous Internship Projects for about 7-8 minutes.
Then there was a general discussion about my personal projects apart from academics( 5-6 minutes ).
Question – 1 : You are given a large array which can consist of only three numbers (1 , 2 and 3). How will you sort them ?
Question – 2 : Now you are given an array of objects. And each object has a field called num which can only take 3 values ( 1, 2 and 3 ) . How will you sort the array of objects ?
Question – 3 : You are given a log csv file. It has the following format for one entry :
10.3.100.1 , 11.4.50.3 , USA
The first entry represent the starting IP address ,the second entry represents the ending IP address and the third entry represents the area in which the range of IP addresses lies. Now given an IP address how will you find the area which the IP address lies ?
Question – 4 : Given a large array of numbers. The operations possible are push a value to the array, delete a value from the array, and search for a value to the array. Which data structure would you choose for performing the operations ?
Question – 5 : Instead now you have only one operation Search which data structure would you prefer ?
Question – 6 : In Java, between HashMap and TreeMap which data structures would you prefer for ideal search and why ?
Question – 7 : Instead of search for a single number now you wish to search a range of numbers which data structure would you prefer ?
Question – 8 : What is a B-Tree ? Which data structure would you prefer B-Tree or BST ?
Question – 9 : Instead of search you can now add or delete a value . Which data structure would you prefer ?