Smallest Window Problem Statement

Given two strings, S and X, your task is to find the smallest substring in S that contains all the characters present in X.

Example:

Input:
S = "abdd", X = "bd"
Output:
"bd"
Explanation:

The substrings in S that contain all the characters in X are: "abdd", "abd", "bdd", "bd". Among these, "bd" is the smallest substring.

Constraints:

  • 1 <= T <= 10
  • 1 <= |S|, |X| <= 10^5
  • Time Limit: 1 sec

Input:

The first line contains an integer T, the number of test cases.
Each test case consists of two lines:
- First line contains the string S.
- Second line contains the string X.

Output:

For each test case, print the smallest window in S containing all characters of X, on a separate line.

Note:

  • There is always a valid window in S that contains all characters of X.
  • If there are multiple smallest windows, return the one that appears first.
AnswerBot
4mo

Find the smallest substring in string S that contains all characters in string X.

  • Iterate through string S and keep track of characters in X found in a window

  • Use two pointers to maintain the window and...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at MakeMyTrip

Q. Given an integer array of size n, find the maximum circular subarray sum. A circ...read more
Q. Design a minimum stack that supports the following operations: push, pop, top, a...read more
Q. Given a linked list, determine if it is a palindrome.
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