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 contains an integer T
representing the number of test cases.
Each test case consists of two lines:
1. A string S
2. A string X
Output:
For each test case, print the smallest window in S
which contains all the characters present in X
, on a separate line.
Example:
Input:
S = "abdd"
X = "bd"
Output:
bd
Explanation:
The windows in S
which contain all the characters in X
are 'abdd', 'abd', 'bdd', 'bd'. The smallest substring in S
which contains all the characters present in X
is 'bd', which is the smallest in terms of length compared to other substrings.
Constraints:
1 ≤ T ≤ 10
1 ≤ |S|, |X| ≤ 105
- Time Limit: 1 sec
Note:
There is always a valid window in S
which contains all the characters of X
. In case of multiple answers, return the substring that occurs first.

AnswerBot
4mo
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 ...read more
Help your peers!
Add answer anonymously...
Microsoft Corporation SDE-2 interview questions & answers
A SDE-2 was asked Q. How would you design a video feed API?
A SDE-2 was asked Q. Given a column number, return its corresponding Excel column address.
A SDE-2 was asked Q. Given a string and a matrix of characters, determine if the string exists in the...read more
Popular interview questions of SDE-2
A SDE-2 was asked Q1. How would you design a video feed API?
A SDE-2 was asked Q2. Given a column number, return its corresponding Excel column address.
A SDE-2 was asked Q3. Given a string and a matrix of characters, determine if the string exists in the...read more
>
Microsoft Corporation SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

