Microsoft Corporation
10+ KWALITY DAIRY (INDIA) Interview Questions and Answers
Q1. Rearrange String Problem Statement
Given a string ‘S’, your task is to rearrange its characters so that no two adjacent characters are the same. If it's possible, return any such arrangement, otherwise return “...read more
Given a string, rearrange its characters so that no two adjacent characters are the same.
Iterate through the string and count the frequency of each character.
Use a priority queue to rearrange the characters based on their frequency.
Check if it's possible to rearrange the string without any two adjacent characters being the same.
Return 'Yes' if possible, 'No' otherwise.
Q2. Find Nodes at Distance K in a Binary Tree
Your task is to find all nodes that are exactly a distance K from a given node in an arbitrary binary tree. The distance is defined as the number of edges between nodes...read more
Find all nodes at distance K from a given node in a binary tree.
Perform a depth-first search starting from the target node to find nodes at distance K.
Use a recursive function to traverse the tree and keep track of the distance from the target node.
Maintain a set to store visited nodes to avoid revisiting them.
Return the list of nodes found at distance K from the target node.
Example: If the target node is 5 and K is 2 in the given tree, the output should be [7, 4, 0, 8].
Q3. My Calendar Problem Statement
Given N
events, each represented with a start
and end
time as intervals, i.e., booking on the half-open interval [start, end). Initially, the calendar is empty. A new event can be ...read more
Given N events with start and end times, determine if each event can be added to the calendar without causing a triple booking.
Iterate through each event and check if adding it causes a triple booking by comparing its interval with previous events
Use a data structure like a list or dictionary to keep track of booked intervals
Return 'True' if the event can be added without causing a triple booking, 'False' otherwise
Q4. Smallest Window Problem Statement
Given two strings S
and X
containing random characters, the task is to find the smallest substring in S
which contains all the characters present in X
.
Input:
The first line co...read more
The task is to find the smallest substring in string S which contains all the characters present in string X.
Iterate through string S and keep track of characters in X using a hashmap
Use two pointers to maintain a sliding window with all characters from X
Update the window size and start index when a valid window is found
Q5. Snake and Ladder Problem Statement
Given a Snake and Ladder Board with 'N' rows and 'N' columns filled with numbers from 1 to N*N starting from the bottom left of the board, and alternating direction each row, ...read more
Find the minimum number of dice throws required to reach the last cell on a Snake and Ladder board.
Use Breadth First Search (BFS) to explore all possible paths with minimum dice throws.
Keep track of visited cells and the number of dice throws needed to reach each cell.
Consider the effect of snakes and ladders on the next position.
Return the minimum number of dice throws needed to reach the last cell.
If it is impossible to reach the last cell, return -1.
Q6. Possible Words from a Phone Number: Problem Statement
Given a string S
composed of digits ranging from 2 to 9, determine all possible strings that can be created by mapping these digits to their corresponding l...read more
Given a phone number string, generate all possible words by mapping digits to letters on a T9 keypad.
Create a mapping of digits to corresponding letters on a T9 keypad
Use recursion to generate all possible combinations of letters for the input phone number
Sort the generated strings in lexicographical order
Q7. Clone Linked List with Random Pointer
Your task is to create a deep copy of a linked list, where each node has two pointers: one that points to the next node in the list, and a 'random' pointer which can point ...read more
Create a deep copy of a linked list with random pointers.
Iterate through the original linked list and create a new node for each node in the list.
Store the mapping of original nodes to their corresponding new nodes.
Update the next and random pointers of the new nodes based on the mapping.
Return the head of the newly created deep copied linked list.
Q8. Connect Nodes at Same Level Problem Statement
Given a binary tree, connect all adjacent nodes at the same level by populating each node's 'next' pointer to point to its next right node. If there is no next righ...read more
Connect adjacent nodes at the same level in a binary tree by populating each node's 'next' pointer.
Traverse the tree level by level using a queue.
For each node, connect it to the next node in the queue.
Set the 'next' pointer of the last node in each level to NULL.
Use constant extra space and do not alter the node structure.
Q9. Next Greater Element Problem Statement
Given a list of integers of size N
, your task is to determine the Next Greater Element (NGE) for every element. The Next Greater Element for an element X
is the first elem...read more
Find the Next Greater Element for each element in a list of integers.
Iterate through the list of integers from right to left.
Use a stack to keep track of elements for which the Next Greater Element is not yet found.
Pop elements from the stack until a greater element is found or the stack is empty.
Assign the Next Greater Element as the top element of the stack or -1 if the stack is empty.
Q10. Minimum Operation Needed to Convert to the Given String
You are given two strings str1
and str2
. Determine the minimum number of operations required to transform str1
into str2
.
Explanation:
An operation is def...read more
Determine the minimum number of operations needed to transform one string into another by moving characters to the end.
Iterate through each character in str1 and check if it matches the first character in str2. If it does, calculate the number of operations needed to move it to the end.
If no match is found for the first character in str2, return -1 as transformation is not possible.
Repeat the process for each test case and output the minimum number of operations needed for ea...read more
Q11. Closest Palindrome Problem Statement
You are given a string 'S' that represents a number. Your task is to find the closest palindromic number to this integer represented by 'S'. The closest number is defined as...read more
Find the closest palindromic number to a given integer represented by a string.
Convert the string to an integer and iterate to find the closest palindromic number.
Check for palindromic numbers by reversing the digits and comparing with the original number.
Handle cases where multiple closest palindromic numbers exist by choosing the smaller one.
Design a system to handle millions of requests for key-value operations efficiently.
Use a distributed key-value store like Redis or Cassandra for storing data.
Implement sharding to distribute data across multiple nodes for scalability.
Use caching mechanisms like Memcached to reduce read latency.
Implement load balancing to evenly distribute incoming requests.
Use asynchronous processing for write operations to improve performance.
Implement data replication for fault tolerance a...read more
A web crawler is a program that systematically browses the internet to index and collect information from websites.
Start by identifying the target websites to crawl
Implement a queue to manage the URLs to be crawled
Use a crawler algorithm to visit and extract data from web pages
Implement a mechanism to handle duplicate URLs and avoid infinite loops
Consider implementing a robots.txt parser to respect website crawling rules
Q14. Find if a given string exists in a given matrix of characters
Find if a given string exists in a given matrix of characters
Iterate through each character in the matrix and check if it matches the first character of the given string. If it does, perform a depth-first search to check if the rest of the string can be formed from adjacent characters in the matrix.
Use a trie data structure to store all possible substrings of the matrix and check if the given string is present in the trie.
Convert the matrix into a string and use string search...read more
Q15. Get excel column address based on number given.
Get Excel column address based on number given.
Divide the number by 26 and get the remainder and quotient.
Convert the remainder to a character and add it to the result string.
Repeat until quotient is zero.
Q16. Video feed api design
Designing a video feed API
Define endpoints for accessing video feeds
Include authentication and authorization mechanisms
Consider scalability and performance
Support different video formats and resolutions
Provide error handling and logging
Ensure data privacy and security
More about working at Microsoft Corporation
Top SDE-2 Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month