Dsa
Sorting
Medium
Maximum Nesting Depth Sorter
You are given several strings containing lowercase letters and parentheses. Sort the strings based on their 'Maximum Nesting Depth' of parentheses (the highest number of nested open parentheses at any point). If depths are equal, sort by the total length of the string, and finally by the string's lexicographical order.
Example:
Input: '(a) (a(b)) ((c))'
- '(a)': Depth 1
- '(a(b))': Depth 2
- '((c))': Depth 2
Output:
(a)
((c))
(a(b))
Key concepts
sortingparsingstack-logic
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now