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

Find the smallest substring in S containing all characters in X.

  • Use a sliding window approach to find the smallest window in S containing all characters in X.

  • Keep track of the characters in X using a ...read more

KALAMALLA VEERA SALIM
3y

Input: string = “this is a test string”, pattern = “tist” Output: Minimum window is “t stri” Explanation: “t stri” contains all the characters of pattern.

Input: string = “geeksforgeeks”, pattern = “o...read more

priyanka b
3y

Input: string = “this is a test string”, pattern = “tist” Output: Minimum window is “t stri” Explanation: “t stri” contains all the characters of pattern.

Input: string = “geeksforgeeks”, pattern = “o...read more

Select
Add answer anonymously...

Flipkart Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Describe a problem that can be solved using the two-pointer technique.
A Software Developer was asked 9mo agoQ. Given the root of a binary search tree (BST) and an integer k, return the kth la...read more
A Software Developer was asked 9mo agoQ. Implement Depth First Search (DFS) traversal on a simple graph.

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. Given the root of a binary search tree (BST) and an integer k, return the kth la...read more
A Software Developer was asked 9mo agoQ2. Implement Depth First Search (DFS) traversal on a simple graph.
A Software Developer was asked Q3. Describe a time you used topological sort to find the order of nodes in a graph.
Flipkart Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits