Tip 1 : Solve atleast 1 DS/Algo problem everyday
Tip 2 : Learn high level designs and low level designs
Tip 1 : Have your skills defined in bold like worked on scaling the system which takes 100 million traffic per day etc, have experience in BigData, kafka, AWS/Azure erc
Tip2 : Show your achievements separately like Won ABC Hackathon, ICPC Regional finalist, etc
I wrote C++as skill, So interviewer asked me how you make sure that whenever dynamically memory is allocated, it gets freed up when you go out of scope from the place where you allocated the memory?
I gave answer that with smart pointer, we would be able to solve this problem.
Then interviewer asked me to code smart pointer class. I made a generic C++ SmartPointer class and overloaded all dereference operators like *, -> etc.
Wrote a destructor which takes care of free up the memory. I did not work in C++ for very long time so my syntax were not very correct but interviewer was happy that I wrote most of the things correctly.



Input: 'a' = [7, 12, 1, 20]
Output: NGE = [12, 20, 20, -1]
Explanation: For the given array,
- The next greater element for 7 is 12.
- The next greater element for 12 is 20.
- The next greater element for 1 is 20.
- There is no greater element for 20 on the right side. So we consider NGE as -1.
I solved this via stack
1. Push the first node to stack.
2. Pick the rest of the node one by one and follow the following steps in the loop:
3. Mark the current node as next node.
4. If the stack is not empty, compare the top node value of the stack with next node value.
5. If next node value is greater than the top node value then, Pop the top node from the stack and next is the next greater element for the popped node.
6. Keep popping the node from the stack while the popped node value is smaller than next node value. next node will becomes the next greater element for all such popped node.
7. Finally, push the next node in the stack.
8. After the loop in step 2 is over, pop all the node from the stack and print 0 as the next element for them.




This is bit tricky because if you you create entire nodes and then copy value of original linked list node then you will shallow copy the references.
I tried to deep copy from end node to head node, but yeah you need to store prev references as those are lost because of previous problem I mentioned above.



Let S = “abdd” and X = “bd”.
The windows in S which contain all the characters in X are: 'abdd', 'abd', 'bdd', 'bd'.
Out of these, the smallest substring in S which contains all the characters present in X is 'bd'.
All the other substring have a length larger than 'bd'.
1. First check if the length of the string is less than the length of the given pattern, if yes then “no such window can exist “.
2. Store the occurrence of characters of the given pattern in a hash_pat[].
we will be using two pointer technique basically
3. Start matching the characters of pattern with the characters of string i.e. increment count if a character matches.
4. Check if (count == length of pattern ) this means a window is found.
5. If such a window found, try to minimize it by removing extra characters from the beginning of the current window.
6. delete one character from first and again find this deleted key at right, once found apply step 5 .
7. Update min_length.
8. Print the minimum length window.1.



class BinaryTreeNode {
int data; // Value of the node.
BinaryTreeNode *left; // Pointer to left child node.
BinaryTreeNode *right; // Pointer to right child node.
BinaryTreeNode *next; // Pointer to next right node at same level.
}
Consider the figure shown below. The left part represents the initial binary tree and right part represents the binary tree after connecting adjacent nodes at the same level.

In the tree shown in the picture above -:
The ‘next’ pointer of the node having value 2 is connected to the node having value 3.
The ‘next’ pointer of the node having value 4 is connected to the node having value 5.
The ‘next’ pointer of the node having value 5 is connected to the node having value 6.
The ‘next’ pointer of nodes having value 1, 3, 6 will have a value NULL as there are no next right nodes in their cases.
1. The structure of the ‘Node’ of a binary tree is already defined. You should not change it.
2. The root of the binary tree is known to you.
3. There is at least one node in the given binary tree.
4. You may only use constant extra space.
I tried multiple approaches like storing direct child in queue and then retrieving and connecting in them and also adding further direct child in queue.
I was not able to solve this completely and some corner case was missing like when there is no right or left child.
But interviewer told me that you tried it nicely.



Let 'S' is 121. Then the nearest integers are 111 and 131 which are palindrome. Both have the same absolute difference but 111 is smaller. Hence 111 is the answer.
I solved it with below steps
1. Break the string into half.
2. Take first half and then reverse this first half and append.
e.g
3745, its first half is 37 and then reversing the first half I will get 73. So next largest no which is palindrome is 3773.
3. Consider a case when you get no. less than the given no. e.g 3796
Following #2 approach you will get 3769 which is less than 3796.
In this case I incremented the first half by 1 and then reversed and appended.
e.g 37+1 = 38, its reverse is 83. So next largest number of given no. which is palindrome is 3883.
4. Yes here you have to take care of even digits and odd digits as well. I was able to handle all cases in code and interviewer was happy.
Here Interviewer asked me to write multiple producers and multiple consumers problem.
Also how you can increase performance of the system
Tip 1 : This is common problem and you can solve this by queue.
Tip 2 : Here to increase performance, I took approack of partitioning the queue so that we dont take lock on entire queue.



A character from an index of a string(str1) is put at the end of it, is defined as a single operation.
You cannot perform any operation on the string, str2.



For a (6 x 6) board, the numbers are written as follows:

You start from square 1 of the board (which is always in the last row and first column). On each square say 'X', you can throw a dice which can have six outcomes and you have total control over the outcome of dice throw and you want to find out the minimum number of throws required to reach the last cell.
Some of the squares contain Snakes and Ladders, and these are possibilities of a throw at square 'X':
You choose a destination square 'S' with number 'X+1', 'X+2', 'X+3', 'X+4', 'X+5', or 'X+6', provided this number is <= N*N.
If 'S' has a snake or ladder, you move to the destination of that snake or ladder. Otherwise, you move to S.
A board square on row 'i' and column 'j' has a "Snake or Ladder" if board[i][j] != -1. The destination of that snake or ladder is board[i][j].
You can only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving - you have to ignore the snake or ladder present on that square.
For example, if the board is:
-1 1 -1
-1 -1 9
-1 4 -1
Let's say on the first move your destination square is 2 [at row 2, column 1], then you finish your first move at 4 [at row 1, column 2] because you do not continue moving to 9 [at row 0, column 0] by taking the ladder from 4.
A square can also have a Snake or Ladder which will end at the same cell.
For example, if the board is:
-1 3 -1
-1 5 -1
-1 -1 9
Here we can see Snake/Ladder on square 5 [at row 1, column 1] will end on the same square 5.
It is very similar to the above problem
The idea is to consider the given snake and ladder board as a directed graph with number of vertices equal to the number of cells in the board. The problem reduces to finding the shortest path in a graph. Every vertex of the graph has an edge to next six vertices if next 6 vertices do not have a snake or ladder. If any of the next six vertices has a snake or ladder, then the edge from current vertex goes to the top of the ladder or tail of the snake. Since all edges are of equal weight, we can efficiently find shortest path using Breadth First Search of the graph.
High level design of Crawler.
Tip 1: Take Queues for background processes, Use bloomfilter to reduce DB query while checking if same URL is already processed, Use different services for page parsing and download pages. You can have separate queues based on domain so that you can parse all pages of same domain once. Use reddis cache to store latest pages.
Tip 2: Use Distributed systems, HDFS for storage large unstructured content and analytical jobs
Tip 3:
Design a system to save key value pair such a way millions of request coming to save/update a key value and millions of request coming to read the value of key.
Some of the tips are here.
Tip 1 : Think about CAP theorem. You have to be consistent here bit of latency is fine.
Tip 2 : Ask about dirty reads are allowed or not. if allowed , you can think on making system faster. Keep the data in cache so that if some old value is fine you can refresh cache after some period of time.
Seperate DB for write and read. Readers can sync from write DB asynchronously.
Multiple producers will be responsible to write data to a disc and multiple consumers will read data to from the disc.
Producers will publish event that comprizes of {DataSizeInBytes and Data}
Here you can think of multiple sectors would be there in a disc. So you can think of allocationg a sector which have sufficient space to write (DataSizeInBytes is part of payload). So you have to take lock on particular sector not on entire disc for making system fast. multiple producers can write on different sectors at the same time and multiple consumers can read from multiple sectors at the same time. Have retry logic for failure handling and replication logic to avoid data loss.

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
How do you remove whitespace from the start of a string?