# Flipkart Interview Experience | Set 32 (For SDE-1)

Flipkart recently visited my college for recruiting SDE-1. Here I am sharing my interview experience. First Round: 2 coding questions on HackerRank (90 minutes). 1. Given some data about computer systems (like RAM, ROM, processor speed, battery, etc). You need to print the top-k computers based on (i) RAM (ii) If RAM is same then ROM (iii) If ROM is same then processor speed, etc. Constraints: k <= 100 (No. of computer systems) n <= 1000000 Solution: Any O(nlogn) sorting would work. But what I did was implement Bubble Sort and ran k-passes of it. Then printed the last k-elements of the array. It took O(nk) time which was okay since k <= 100. 2. There is a maze of size n*n. Tom is sitting at (0,0). Jerry is sitting in another cell (the position of Jerry is input). Then there are k pieces of cheese placed in k different cells (k <= 10). Some cells are blocked while some are not. Tom can move to 4 cells at any point of time (left, right, up, down one position). Tom has to collect all the pieces of cheese and then reach to Jerry’s cell. You need to print the minimum no. of steps required to do so. Solution: Dynamic Programming. Second Round: Around 1 hour (Face-to-Face) 1. Given a variation of singly linked list, where every node can have 2 pointers: next and down. You need to flatten the list into a singly linked list in which every node has only a next pointer. This needs to be done in-place, so the down pointer of every node should be set to null. Example:
```1 -> 2 -> 3 -> 4
|         |
V         V
7 -> 8    10 -> 11
|
V
14 -> 15 -> 16
|
V
20 -> 21
Output:
1 -> 7 -> 14 -> 15 -> 20 -> 21 -> 16 -> 8 -> 2 -> 3 -> 10 -> 11 -> 4```
Solution: I first told him to do it iteratively using stack. He said that its too complex. I then told him to use recursion, which was about 2 lines of code. He asked me to code it and was completely satisfied. Remember to cover all the base cases and the corner cases. 2.1. You are given a string. Print the first non-repeating character. Solution: I asked him the character encoding. He said ASCII. I told him to take an integer array “count” of size 128 with every element set to 0. Scan the string and for every character ‘c’, do count[(int)c]++. Then scan the string again and when you find the first character for which count[(int)c] == 1, print it and break out. If no such character found whose count[(int)c] == 1, that means no answer (no non-repeating character in the string). 2.2. What if the string is very long. Doing two passes is expensive. Solution: I would use an array of structure with two elements count and index. When we do count[(int)c]++, we will also set index[(int)c] = index of c in the string. Now instead of scanning the string second time, scan the structure array (for i = 0 to 127) and keep a temporary variable, tempIndex = MAX_INTEGER. When you find count[i] == 1, check if index[i] < tempIndex. If true, update tempIndex to index[i]. In the end print the character in the string at index = tempIndex. If tempIndex == MAX_INTEGER, that means no answer (no non-repeating character in the string). 2.3. You are not given a string. You are given some queries of Type 1 and Type 2. Type 1: Append character (given as input) to the end of the string. The string is initially empty. Type 2: Print the first non-repeating character in the string that is formed so far. Solution: Keep on constructing the string for every Type 1 query. That would take O(n) time for every Type 1 query on a non-mutable string. If we use a mutable data type, like StringBuilder in case of Java, it would take O(1) time for every Type 1 query. For Type 2 query, run the algorithm of 2.2 (above). That would take O(n) time for every Type 2 query. If there are q Type 2 queries, it will take O(nq) time. 2.4. Solve 2.3 (above) in constant time, i.e. for every query (of Type 1 or Type 2) take constant time. So overall complexity should be O(q) where q = no. of Type 1 queries + no. of Type 2 queries. Solution: Took a lot of time (around 20 minutes), and tried a lot of stuff before arriving at the below solution.
• Construct a queue using a doubly linked list (singly linked list can also be used with some modification in the below algorithm, but the interviewer said not to bother).
• Construct a hashMap with Key = character, and two values. Value 1: boolean isPresentInQueue, Value 2: Pointer to a node in linked list.
• When you receive a character (in Type 1 Query), check if that character is present in hashMap.
• If not present, insert it. Set isPresentInQueue to true. Insert the character to the end of the queue and keep the pointer (address) of the inserted node in the Value 2 part of the hashMap (for that particular character). This will take O(1) time.
• If the character is present in hashMap and isPresentInQueue is set to true for that character, set isPresentInQueue to false. Using the pointer (address) in the Value 2 part of the hashMap (for that particular character), delete the node from the linked list. This will take O(1) time.
• If the character is present in hashMap and isPresentInQueue is false, ignore the character and move forward. This will take O(1) time.
• When you receive a Type 2 Query, just print the front (head) of the linked list. That would be the answer. This will take O(1) time.