Unlock the Top 20 Coding Questions to Pass Microsoft Interview
Landing a job at Microsoft is a dream for many software engineers. Known for its innovation, competitive salary, and dynamic work culture, Microsoft is a company where you can truly make an impact.
But, like any top-tier tech company, getting through their interview process requires preparation and practice.
Whether you're a seasoned developer or a fresh graduate, preparing for Microsoft’s coding interviews can be a daunting task.
The key is to focus on the right set of problems and understand what the company is looking for in a candidate.
In this blog, we’ll walk you through the top 20 coding questions that can help you ace your Microsoft interview, along with insights into the interview format, types of questions, and tips on what Microsoft really values in their candidates.
Section 1: Understanding Microsoft’s Interview Process
Microsoft was started in 1975 by Bill Gates and Paul Allen. They created software for personal computers. Their big break came with the release of MS-DOS in the early 1980s.
Later, they launched Windows, which became very popular and made computers easier to use.
Over the years, Microsoft has created many successful products, including Office, Xbox, and Azure.
Today, it is one of the largest tech companies in the world, known for its innovation and impact on the tech industry.
Format of the Interview
Microsoft's interview process typically consists of several rounds. Here's a breakdown:
-
Initial Screening: This is usually a phone or video call where a recruiter will assess your background, experience, and basic fit for the role.
-
Technical Interviews: These can be a mix of online coding tests, phone interviews with coding questions, and sometimes a system design interview.
-
On-site Interviews: Due to remote working, these are often conducted virtually now. Expect multiple rounds where you’ll tackle coding problems, system design questions, and behavioral questions.
Types of Questions
The questions in Microsoft interviews fall into three main categories:
-
Coding Problems: These focus on your problem-solving skills and understanding of data structures and algorithms. You might be asked to solve problems using arrays, strings, linked lists, trees, graphs, etc. Check out the top Leetcode patterns for FAANG coding interviews.
-
System Design: Here, the interviewer wants to see how you design complex systems. It involves understanding components, databases, scalability, and performance. Discover the 18 essential system design concepts for the interview.
-
Behavioral Questions: These questions assess your soft skills, teamwork, and how you handle various work scenarios.
What Microsoft Looks For
Microsoft seeks candidates who are not only technically proficient but also fit well within their team culture. They value:
-
Problem-Solving Skills: How you approach and solve problems is crucial. Clear, logical thinking and the ability to break down problems are essential.
-
Coding Efficiency: Clean, efficient, and readable code is highly valued. Make sure your code is easy to understand and well-structured.
-
Collaboration: Teamwork and communication skills are important. Show that you can work well with others and contribute positively to a team.
-
Growth Mindset: Microsoft appreciates individuals who are eager to learn and grow. Show that you’re open to feedback and continuously improving your skills.
Section 2: 20 Coding Problems To Pass Microsoft Interview
Here are the top 20 coding problems you need to practice for your Microsoft interview:
1. Buddy Strings
Problem Statement: Given two strings a and b, return true if they can be considered "buddy strings". Otherwise, return false. Two strings are buddy strings if you can swap any two letters in one string to make it equal to the other string.
Example:
- Input: a = "xy", b = "yx"
- Expected Output: true
- Justification: Swapping 'x' and 'y' in string a results in "yx", which matches string b.
Difficulty Level: Easy
Solution: Buddy Strings
2. Can Place Flowers
Problem Statement: You have a long, narrow flowerbed divided into sections, each of which can either be empty or filled with a flower.
Due to gardening restrictions, you cannot plant flowers in adjacent sections — they must be at least one section apart to prevent overcrowding.
Given the current state of the flowerbed (represented as an array, where 0 indicates an empty section and 1 signifies a section with a flower) and a number representing how many more flowers you wish to plant, determine if you can plant all the desired flowers without breaking the gardening restrictions.
Example:
- Input: flowerbed = [0,0,1,0,1,0,0], n = 2
- Expected Output: true
- Justification: You can plant flowers in the 0th and 7th index, satisfying the non-adjacent rule.
Difficulty Level: Easy
Solution: Can Place Flowers
3. Reverse a LinkedList
Problem Statement: Given the head of a Singly LinkedList, reverse the LinkedList. Write a function to return the new head of the reversed LinkedList.
Example:
Constraints:
- The number of nodes in the list is the range [0, 5000].
- -5000 <= Node.val <= 5000
Difficulty Level: Easy
Solution: Reverse a LinkedList
4. Maximum Number of Balloons
Problem Statement: Given a string, determine the maximum number of times the word "balloon" can be formed using the characters from the string. Each character in the string can be used only once.
Example:
- Input: "balloonballoon"
- Expected Output: 2
- Justification: The word "balloon" can be formed twice from the given string.
Difficulty Level: Easy
Solution: Maximum Number of Balloons
5. Binary Tree Path Sum
Problem Statement: Given a binary tree and a number ‘S’, find if the tree has a path from root-to-leaf such that the sum of all the node values of that path equals ‘S’.
Example:
- Input: S= 10
- Expected Output: true
- Justification: The path with sum ‘10’ is highlighted
Difficulty Level: Easy
Solution: Binary Tree Path Sum
6. Min Cost Climbing Stairs
Problem Statement: Given an array of integers cost, where cost[i] represents the cost of the ith step on a staircase, determine the minimum total cost to reach the top of the staircase.
It is given that once you pay the cost, you can climb the stairs by either taking one step or two steps at a time. You can start stepping either from the 0 or 1 index.
Example:
- Input: [2, 7, 9, 3, 1]
- Expected Output: 10
- Justification: The minimum cost path starts on the second step (cost=7), then jumps two steps to the fourth step (cost=3), and finally reaches the end, totaling a cost of 10.
Difficulty Level: Easy
Solution: Min Cost Climbing Stairs
7. Intersection of Two Arrays
Problem Statement: Given two arrays, nums1 and nums2 containing integers, return an array of the intersection of nums1 and nums2. In the resultant array, each integer should be unique, and elements can be in any order.
Example:
- Input: nums1 = [3,1,2,6,7,8], nums2 = [2,3,4,5,7,9]
- Expected Output: [2,3,7]
- Justification: The numbers 2, 3, and 7 are present in both nums1 and nums2.
Difficulty Level: Easy
Solution: Intersection of Two Arrays
8. Find the Index of the First Occurrence in a String
Problem Statement: Given two strings str1 and str2, return the index of first occurrences of the str2 string in str1. If str2 doesn't exists in str1 string, return -1.
Example:
- Input: str1 = "algorithms", str2 = "rith"
- Expected Output: 4
- Justification: The substring "rith" begins at index 4 in the string "algorithms".
Difficulty Level: Easy
Solution: Find the Index of the First Occurrence in a String
9. Merge Two Sorted Lists
Problem Statement: Given the head of two sorted linked lists, l1 and l2. Return a new sorted list created by merging together the nodes of the first two lists.
Example:
- Input: [1, 3, 5][2, 4, 6]
- Expected Output: [1, 2, 3, 4, 5, 6]
- Justification: Merging the two sorted linked lists, node by node, results in a single sorted linked list containing all elements from both input lists.
Difficulty Level: Easy
Solution: Merge Two Sorted Lists
10. Nth Digit
Problem Statement: Given a positive integer n, find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13… and so on.
Example:
- Input: n=3
- Expected Output: 3
- Justification: The sequence starts as 1, 2, 3… so the third digit is 3.
Difficulty Level: Medium
Solution: Nth Digit
11. Find Original Array From Doubled Array
Problem Statement: A doubled array changed is formed from the original array after appending twice the value of each element in the original array and randomly shuffling elements in the updated original array.
Given an array changed, return the original array if changed is a valid doubled array. Otherwise, return empty array.
Example:
- Input: changed = [2,4,1,8]
- Expected Output: [1,4]
- Justification: The original array [1,4] is doubled to form [2,4,1,8] (1's double is 2, and 4's double is 8).
Difficulty Level: Medium
Solution: Find Original Array From Doubled Array
12. Longest Arithmetic Subsequence
Problem Statement: Given an array nums containing positive integers, return the maximum length of a subsequence that forms an arithmetic progression.
-
A subsequence is an array that can be formed from nums by deleting 0 or more elements without changing the order of the remaining elements.
-
An arithmetic progression (AP) is a sequence of numbers in which the difference between any two consecutive elements is constant. In short, seq[i + 1] - seq[i] should be same for all 0 <= i < seq.length - 2.
Example:
- Input: [8, 12, 6, 4, 2]
- Expected Output: 4
- Justification: The subsequence [8, 6, 4, 2] forms the longest arithmetic subsequence with a common difference of -2, thus the longest arithmetic subsequence has a length 4.
Difficulty Level: Medium
Solution: Longest Arithmetic Subsequence
13. Score of Parentheses
Problem Statement: Given a balanced parenthesis string s, return the score of the given string.
Follow the below rules to determine the score of the string s.
- "()" has score 1.
- XY has score X + Y, where X and Y both are balanced parentheses strings.
- (X) has score 2 * X, where X is a balanced parentheses string.
Example:
- Input: "((()))"
- Expected Output: 4
- Justification: The string represents a pair of parentheses inside another pair, which is again inside another pair. This structure results in a score of ((())) = ((12)2) = 4.
Difficulty Level: Medium
Solution: Score of Parentheses
14. Inorder Successor in BST
Problem Statement: Given a root node of the binary search tree and node p, return the value of the in-order successor of node p in the given tree. If the given node has no in-order successor, return -1.
The in-order successor of a node is the next node in the in-order traversal of the BST, which means it is the node with the smallest value greater than the given node.
Example:
- Input: root = [3,2,4,1], p = 2
- Expected Output: 3
- Justification: In the in-order traversal [1,2,3,4], the next node after 2 is 3, making it the in-order successor.
Difficulty Level: Medium
Solution: Inorder Successor in BST
15. Rotate Image
Problem Statement: Given an n x n 2D matrix, modify a square matrix by rotating it 90 degrees in a clockwise direction.
Note: This rotation should be done in-place, meaning the transformation must occur within the original matrix without using any additional space for another matrix.
Example:
- Input: matrix =[[1,2], [3,4]]
- Expected Output: [[3,1], [4,2]]
- Justification: After rotating the 2x2 matrix 90 degrees to the right, the element at the top left (1) moves to the top right, the top right (2) moves to the bottom right, the bottom right (4) moves to the bottom left, and the bottom left (3) moves to the top left.
Difficulty Level: Medium
Solution: Rotate Image
16. Number of Islands
Problem Statement: Given a 2D array (i.e., a matrix) containing only 1s (land) and 0s (water), count the number of islands in it.
An island is a connected set of 1s (land) and is surrounded by either an edge or 0s (water). Each cell is considered connected to other cells horizontally or vertically (not diagonally).
Example:
- Input: matrix =
- Expected Output: 1
- Justification: The matrix has only one island. See the highlighted cells below.
Difficulty Level: Medium
Solution: Number of Islands
17. Regular Expression Matching
Problem Statement: Given a string text and regular expression pattern, return true if string text matches with pattern. Otherwise, return false.
Follow the below rules to match text with pattern.
- '.' matches any single character.
- '*' matches zero or more occurrences of the preceding element.
Example:
- Input: text = "abc", pattern = "a.c"
- Expected Output: True
- Justification: The '.' in the pattern matches any character, so "a.c" matches "ABC".
Difficulty Level: Hard
Solution: Regular Expression Matching
18. Largest Rectangle in Histogram
Problem Statement: Given an array of integers heights, where heights[i] represents the height of the histogram's bar, return the area of the largest rectangle in the histogram. Each bar has width equal to 1.
Example:
- Input: [4, 2, 3, 2]
- Expected Output: 8
- Justification: The largest rectangle can be drawn from the first bar to the fourth bar, having height 2, resulting in an area of 2x4=8.
Difficulty Level: Hard
Solution: Largest Rectangle in Histogram
19. Median of Two Sorted Arrays
Problem Statement: Given two sorted arrays, nums1 and nums2 of different sizes in the ascending order, return the median of two sorted arrays after merging them.
The median is the middle value in an ordered set, or the average of the two middle values if the set has an even number of elements.
Example:
- Input: [1, 3, 5] and [2, 4, 6]
- Expected Output: 3.5
- Justification: When merged, the array becomes [1, 2, 3, 4, 5, 6]. The median is the average of the middle two values, (3 + 4) / 2 = 3.5.
Difficulty Level: Hard
Solution: Median of Two Sorted Arrays
20. Longest Valid Parentheses
Problem Statement: You are given a string containing just the characters '(' and ')'. Your task is to find the length of the longest valid (well-formed) parentheses substring.
Example:
- Input: "(())"
- Expected Output: 4
- Justification: The entire string is a valid parentheses substring.
Difficulty Level: Hard
Solution: Longest Valid Parentheses
For more information, find the 12 coding interview FAQs.
Wrapping Up
Preparing for a Microsoft interview can be challenging, but with the right approach, it’s certainly achievable.
Focus on understanding the interview format, make sure to properly practice the type of questions covered in this blog, and demonstrate the skills that Microsoft values.
To prepare Microsoft coding interview under expert guidance, check out Grokking the Microsoft Coding Interview by DesignGurus.io