DSA interview question
You're building a syntax auto-repair tool for an in-house config-file linter (think: JSON-like or expression configs where brackets occasionally get mistyped due to copy-paste errors or merge conflicts). Given a string consisting only of { and } characters, your tool needs to figure out the minimum number of bracket reversals (flipping a { to } or vice versa) needed to make the expression balanced. If the total length of the string is odd, it's impossible to balance — return -1 in that case.
Example: s = "}{{}}{{{" → minimum 3 reversals needed.
int minBracketReversals(string s);
Input s | Output | Explanation |
|---|---|---|
"}{{}}{{{" | 3 | Needs 3 flips to balance |
"{{}}" | 0 | Already balanced |
"}}}}" | 2 | Flip two } to { → "{{}}" style balance, 4/2=2 flips |
"}{" | 2 | Single mismatched pair needs 2 flips (one {-type, one }-type imbalance) |
"{{{" | -1 | Odd length — impossible to balance |
"" | 0 | Empty string is trivially balanced |
"{}" | 0 | Already balanced |
"}{}{" | 2 | Needs 2 flips |
(Note: this problem assumes only { and } — no need to track multiple bracket types like ()[].)
{ and }, find the minimum number of bracket flips needed to balance it."{ and } (no other bracket types like ()[]mixed in)? (Confirm — this problem is specifically simplified to one bracket type.){ to } (and vice versa), not insert/delete characters? (Confirm — this is distinct from "minimum insertions to balance," a different problem.)-1, since balance requires equal counts of some even total length — confirm.){/} characters), or do I need to validate/sanitize input first?}} pair and a leftover {{ pair into a single reversal count."() and []? How would your approach change?"On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in