cswork / libraryOpen practice workspace

Coding questions · Boston Consulting Group

Implement palindrome check and valid parentheses

This question evaluates string-processing skills, algorithmic reasoning, and attention to edge cases by asking for a palindrome detector and a parentheses-sequence validator.

You have 20–25 minutes per problem. Implement the following two functions and be prepared to explain your approach and time/space complexity.

Problem 1 — Palindrome Check

Write a function that determines whether a given string is a palindrome.

  • A string is a palindrome if it reads the same forward and backward.
  • Compare characters exactly as they appear (no case folding and no ignoring punctuation/whitespace unless you explicitly state and implement it).

Function signature (example):

  • bool is_palindrome(s: str)

Input:

  • s: a string (may be empty)

Output:

  • true if s is a palindrome, otherwise false

Examples:

  • "abba" -> true
  • "abc" -> false
  • "" -> true

Problem 2 — Valid Parentheses Sequence

Write a function that checks whether a string containing only parentheses characters is valid.

A string is valid if:

  1. Every opening bracket has a corresponding closing bracket of the same type.
  2. Brackets close in the correct order.

Assume the allowed bracket types are: (), [], {}.

Function signature (example):

  • bool is_valid_parentheses(s: str)

Input:

  • s: a string consisting only of characters in ()[]{} (may be empty)

Output:

  • true if s is valid, otherwise false

Examples:

  • "()" -> true
  • "([]){}" -> true
  • "(]" -> false
  • "([)]" -> false
  • "" -> true

Documentation requirement

For each function, include a docstring with:

  • Description
  • Inputs/Outputs
  • Callouts (assumptions, edge cases, complexity)

Exact Palindrome Check

Return whether a string reads the same forward and backward, comparing every character exactly.

Examples

Example 1

Input: ('abba',)
Output: True

Explanation: Even palindrome.

Example 2

Input: ('abc',)
Output: False

Explanation: Not palindrome.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.

Valid Parentheses Sequence

Return whether a bracket string using (), [], and {} is balanced and closes in the correct order.

Examples

Example 1

Input: ('()',)
Output: True

Explanation: Simple pair.

Example 2

Input: ('([]){}',)
Output: True

Explanation: Nested and adjacent.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.