Words by Curly Brace Depth
Updated
Problem statement
The string s holds words made of lowercase letters, curly braces { and }, and spaces. The braces are balanced and every word sits inside at least one pair. A word's depth is the number of brace pairs around it.
Return a list whose entry d (counting from 0) lists every word at depth d + 1, in the order the words appear in s. The list has one entry for each depth from 1 up to the deepest pair in s; a depth with no words gets an empty list.
Spaces only separate words. A brace can sit right next to a word.
Function
wordsByDepth(s: String) → List<List<String>>Examples
Example 1
s = "{ hello { how are you } }"return = [["hello"],["how","are","you"]]hello is inside one pair of braces, so it is at depth 1. how, are and you are inside two pairs, so they are at depth 2.
Example 2
s = "{ card {debit} { credit { gold } } fee }"return = [["card","fee"],["debit","credit"],["gold"]]Depth 1 holds card and fee; fee comes after the inner groups close. Depth 2 holds debit and credit, from two different inner groups. Depth 3 holds only gold. A brace does not need a space next to it.
Example 3
s = "{{ deep }}"return = [[],["deep"]]The outer pair holds no word of its own, so depth 1 is an empty list. deep sits at depth 2.
Constraints
2 <= s.length <= 2000scontains only lowercase English letters,{,}and spaces.- The braces in
sare balanced,shas at least one pair, and every letter is inside at least one pair.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Parse Nested HTML into a TreeApple · ONSITE INTERVIEWApple interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Lisp Expression ParserAmazon · ONSITE INTERVIEW
- Lisp Expression ParserAttentive · ONSITE INTERVIEWAttentive interview questions
- Flatten Nested Map Paths with a StackSalesforce · ONSITE INTERVIEWSalesforce interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences