Microsoft Recent SDE2 DSA Queations
Summary
I've compiled a set of Data Structures and Algorithms questions that were recently posed in SDE2 interviews at Microsoft, shared by myself and my friends.
Full Experience
This post is a collection of Data Structures & Algorithms questions that I and a few of my friends encountered during our recent SDE2 interviews at Microsoft. Our aim is to provide valuable insights into the types of problems asked for this role, ranging from standard LeetCode problems to custom algorithmic challenges and even a system design question. These questions were gathered from various rounds we faced, including some with direct LeetCode links and others described in detail. Hopefully, this comprehensive list will aid others preparing for similar roles.
Interview Questions (16)
Minimize String Partitions
You are given a string s = "31415926535 8979323846264338327" and a list of words: ["314", "15926535 89", "79323846264", "338327", "7932", "3846264", "338327"]. Your task is to partition the string s into substrings such that:
- Each substring is present in the given list of words.
- The number of partitions (substrings) is minimized.
Time Based Key-Value Store
Design a time-based key-value store that supports set and get operations. The set operation stores a key with a value and a timestamp. The get operation retrieves the value associated with a key at a specific timestamp, returning the most recent value if multiple exist before or at the given timestamp.
Determine if Two Strings Are Close with Word Ladder Follow-up
Determine if two strings are 'close' based on specific operations (swap any two existing characters, or transform every occurrence of one existing character into another existing character, and vice versa). A follow-up problem, similar to 'Word Ladder', was also discussed.
Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. The length of num is less than 10002 and will be >= k.
Spiral Matrix
Given an m x n matrix, return all elements of the matrix in spiral order.
Design Text Editor with Undo/Redo
Design a text editor that supports basic operations like typing, deleting, and cursor movement, along with undo and redo functionalities. Several follow-up questions were also part of this problem.
3Sum
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets.
Subarray Sum Equals K
Given an array of integers nums and an integer k, return the total number of contiguous subarrays whose sum equals to k.
Group Anagrams
Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Find All Peak Elements in Array
Given an integer array nums, find all the peak elements. A peak element is an element that is strictly greater than its neighbors. If there are multiple peaks, return all of them.
Valid Parentheses
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type.
Sort Array by Set Bit Count
Given an integer array, sort the integers in ascending order based on the count of their set bits (1s in binary representation). If two numbers have the same count of set bits, maintain their relative order as in the original array (or sort by value if not specified, but typically stability is preferred).
Remove Spaces from Character Vector In-place
Given a mutable sequence of characters (e.g., std::vector<char> or a character array), remove all spaces from it in-place. The relative order of non-space characters must be preserved.
Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. The given node will not be the tail and it will always be a valid node of the linked list.
Reconstruct Itinerary
You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and arrival airports of one flight. Reconstruct the itinerary in order. All of the tickets belong to a man who departs from 'JFK'. Thus, the itinerary must begin with 'JFK'. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"]. All airports are represented by three uppercase English letters. You may assume that there exists at least one valid itinerary.
Maximum Profit of Contiguous Subarray (Length at most K)
You are given an integer array pnl of length n, where pnl[i] represents the profit or loss on day i. You are also given an integer k. Your task is to find the maximum possible profit of any contiguous subarray whose length is at most k.