Microsoft interview experience Real time questions & tips from candidates to crack your interview

SDE - 2

Microsoft
upvote
share-icon
5 rounds | 12 Coding problems

Interview preparation journey

expand-icon
Preparation
Duration: 6 Months
Topics: Graph Algorithms(BFS,DFS), Greedy Programming, Dynamic Programming,Problems involving Arrays, LinkedList, Queues, Stacks,High Level Designs and Low Level Designs.
Tip
Tip

Tip 1 : Solve atleast 1 DS/Algo problem everyday
Tip 2 : Learn high level designs and low level designs

Application process
Where: Other
Eligibility: 5+ years of experience.
Resume Tip
Resume tip

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

Interview rounds

01
Round
Easy
Face to Face
Duration60 Minutes
Interview date7 Feb 2019
Coding problem3

1. C++ Question

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?

Problem approach

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.

2. Next Greater Element

Easy
10m average time
90% success
0/40
Asked in companies
IBMInfo Edge India (Naukri.com)Amazon

You are given an array 'a' of size 'n'.



The Next Greater Element for an element 'x' is the first element on the right side of 'x' in the array, which is greater than 'x'.


If no greater elements exist to the right of 'x', consider the next greater element as -1.


For example:
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.
Problem approach

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.

Try solving now

3. Clone Linked List with Random Pointer

Easy
10m average time
90% success
0/40
Asked in companies
MicrosoftUrban Company (UrbanClap)Amazon

Given a linked list having two pointers in each node. The first one points to the next node of the list, however, the other pointer is random and can point to any node of the list or null. The task is to create a deep copy of the given linked list and return its head. We will validate whether the linked list is a copy of the original linked list or not.

A deep copy of a Linked List means we do not copy the references of the nodes of the original Linked List rather for each node in the original Linked List, a new node is created.

For example,

example

Random pointers are shown in red and next pointers in black.

Problem approach

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.

Try solving now
02
Round
Medium
Face to Face
Duration60 Minutes
Interview date7 Feb 2019
Coding problem2

1. Smallest Window

Moderate
10m average time
90% success
0/80
Asked in companies
HSBCSAP LabsExpedia Group

You are given two strings S and X containing random characters. Your task is to find the smallest substring in S which contains all the characters present in X.

Example:

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'.
Problem approach

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.

Try solving now

2. Connect Nodes at Same Level

Moderate
30m average time
70% success
0/80
Asked in companies
Expedia GroupMicrosoftOla

A binary tree is a tree where each node has at most two children i.e left child and right child.

You are given a binary tree, where the structure of the node is as follow -:

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. 
}

Your task is to connect all the adjacent nodes at the same level in the given binary tree. You can do this by populating each 'next' pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all the next pointers are set to NULL.

For Example:

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.

alt text

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.

Note:

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.
Problem approach

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.

Try solving now
03
Round
Easy
Face to Face
Duration60 Minutes
Interview date7 Feb 2019
Coding problem2

1. Nearest Pallindrome

Moderate
30m average time
70% success
0/80
Asked in companies
eBayAppleMicrosoft

You have been given a string ‘S' representing a number. Your task is to find the closest palindromic number from this integer represented by 'S'. The closest number is defined as the one for which the absolute difference between the given integer represented by 'S' and the palindrome number is minimum. If more than one number have the same difference then return the smaller integer.

Example:

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.
Problem approach

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.

Try solving now

2. System Design Question

Here Interviewer asked me to write multiple producers and multiple consumers problem.
Also how you can increase performance of the system

Problem approach

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.

04
Round
Medium
Face to Face
Duration60 Minutes
Interview date7 Feb 2019
Coding problem3

1. Minimum operation needed to convert to the given string

Moderate
26m average time
0/80
Asked in companies
SamsungAppleMicrosoft

You are given two strings 'str1' and 'str2'. Find the minimum operations required to convert str1 into str2.

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

2. Snake and Ladder

Moderate
30m average time
60% success
0/80
Asked in companies
MeeshoVisaMicrosoft

You have been given a Snake and Ladder Board with 'N' rows and 'N' columns with the numbers written from 1 to (N*N) starting from the bottom left of the board, and alternating direction each row.

For example

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

6*6 Board

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].
Note :
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.
Problem approach

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.

Try solving now

3. System Design Question

High level design of Crawler.

Problem approach

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:

05
Round
Medium
Face to Face
Duration70 Minutes
Interview date8 Feb 2019
Coding problem2

1. System Design Question

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.

Problem approach

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.

2. System Design Question

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}

Problem approach

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

Skill covered: Programming

How do you remove whitespace from the start of a string?

Choose another skill to practice
Similar interview experiences
company logo
SDE - 2
3 rounds | 9 problems
Interviewed by Microsoft
2056 views
0 comments
0 upvotes
company logo
SDE - 2
3 rounds | 5 problems
Interviewed by Microsoft
1651 views
0 comments
0 upvotes
company logo
SDE - 2
5 rounds | 7 problems
Interviewed by Microsoft
1649 views
0 comments
0 upvotes
company logo
SDE - 2
3 rounds | 5 problems
Interviewed by Microsoft
7377 views
0 comments
0 upvotes
Companies with similar interview experiences
company logo
SDE - 2
5 rounds | 12 problems
Interviewed by Walmart
29892 views
8 comments
0 upvotes
company logo
SDE - 2
3 rounds | 5 problems
Interviewed by Amazon
6765 views
1 comments
0 upvotes
company logo
SDE - 2
6 rounds | 8 problems
Interviewed by Amazon
5280 views
0 comments
0 upvotes