Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Remove Invalid Parentheses

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Remove Invalid Parentheses. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Remove the minimum number of invalid parentheses from a string and return all valid results.

Asked in the Coding stage. Algorithmic coding task focused on search space and string manipulation.

Task

Write a PostgreSQL query that reads candidate Instagram Reels captions from a table and returns every caption that is valid after removing the minimum number of parentheses.

Requirements

  1. Treat only ( and ) as parentheses, all other characters are literal text.
  2. Return every distinct valid result that can be formed with the minimum removals.
  3. If multiple captions have the same minimum removals, return all of them.
  4. Order results by removed_count ascending, then valid_caption ascending.

Schema

reels_caption_candidates
ColumnTypeDescription
candidate_idPKINTUnique identifier for the caption candidate
caption_textTEXTInstagram Reels caption text to validate
Tablesreels_caption_candidates
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results