Balanced Sequence After Replacement

Given a string of length 'N' containing only the characters: '[', '{', '(', ')', '}', ']'. At certain places, the character 'X' appears in place of any bracket. Your goal is to determine if replacing all the 'X's with suitable brackets can result in a valid balanced sequence.

Example:

Input:
"[X)](X"
"[XX{"
Output:
Valid
Invalid
Explanation:

For the string "[X)](X", replacing the first X with '(' and the second X with ')' results in the sequence "[()]()", which is a valid balanced sequence. However, for the string "[XX{", no replacement will result in a valid bracket sequence.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 20
  • Time limit: 1 sec

Input:

The first line contains an integer 'T' indicating the number of test cases. Each of the following T lines contains a single string to be evaluated.

Output:

For each test case, output "Valid" if a balanced sequence can be achieved after replacing all 'X's, otherwise output "Invalid".

Note:

In the function, return 'True' for a valid string and 'False' for an invalid string without printing.

AnswerBot
4mo

Determine if a valid balanced sequence can be achieved by replacing 'X's with suitable brackets.

  • Iterate through the string and keep track of the count of opening and closing brackets.

  • If at any point t...read more

Help your peers!
Select
Add answer anonymously...

Cohesity Software Developer interview questions & answers

A Software Developer was asked 11mo agoQ. What is a linked list?
A Software Developer was asked 11mo agoQ. You are given two integer arrays nums1 and nums2, sorted in non-decreasing order...read more
A Software Developer was asked Q. Balanced Sequence After Replacement Given a string of length 'N' containing only...read more

Popular interview questions of Software Developer

A Software Developer was asked 11mo agoQ1. What is a linked list?
A Software Developer was asked 11mo agoQ2. You are given two integer arrays nums1 and nums2, sorted in non-decreasing order...read more
A Software Developer was asked Q3. Balanced Sequence After Replacement Given a string of length 'N' containing only...read more
Cohesity 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