Unit 7 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
True
.HAPPY CASE
Input: "(())"
Output: True
Explanation: The string is a correctly nested pair of parentheses.
EDGE CASE
Input: "(()"
Output: False
Explanation: The string has an unmatched opening parenthesis.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
This problem is a typical string parsing problem that can be elegantly solved with recursion:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a recursive function to validate nested parentheses by checking pairs from the outside inward.
1) If the string is empty, return True (base case).
2) If the string starts with '(' and ends with ')', recursively validate the substring between them.
3) Otherwise, return False.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def is_nested_parens(s):
if s == ":
return True
if len(s) >= 2 and s[0] == '(' and s[-1] == ')':
return is_nested_parens(s[1:-1])
return False
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(n)
because each recursive call processes two fewer characters until the string is empty or invalid.O(n)
due to the recursion stack depth, potentially extending to the length of the string in deeply nested cases.