cswork / libraryOpen practice workspace

Coding questions · Meta

Solve tree view and parentheses cleanup

This question evaluates binary tree traversal and view-extraction skills for the right-side view problem and string-processing and minimal-edit balancing for the parentheses cleanup, probing competency in data-structure traversal, state tracking, and correctness under edge cases.

Solve the following two algorithm problems.

  1. Right-side view of a binary tree

Given the root of a binary tree, return the values of the nodes that would be visible if you looked at the tree from its right side, listed from top to bottom.

Example:

  • Input: root = [1,2,3,null,5,null,4]
  • Output: [1,3,4]
  1. Remove the minimum invalid parentheses

Given a string s containing lowercase English letters and the characters ( and ), remove the minimum number of parentheses so that the resulting string is valid.

A string is valid if:

  • every opening parenthesis has a matching closing parenthesis,
  • parentheses are properly ordered,
  • letters may appear anywhere and do not affect validity.

Return any valid string after the minimum removals.

Examples:

  • Input: a)b(c)d Output: ab(c)d
  • Input: ))( ( Output: ``
  • Input: (a(b(c)d) Output: a(b(c)d)

Right-Side View of a Binary Tree

Given level-order tree values, return the values visible from the right side.

Examples

Example 1

Input: ([1,2,3,None,5,None,4],)
Output: [1, 3, 4]

Explanation: Prompt example.

Example 2

Input: ([],)
Output: []

Explanation: Empty tree.

Hints

  1. Model object-style prompts as arrays or operation streams when needed.
  2. Handle empty and boundary cases before the main logic.

Remove Minimum Invalid Parentheses

Remove the minimum number of parentheses so the remaining string is valid.

Examples

Example 1

Input: ('a)b(c)d',)
Output: 'ab(c)d'

Explanation: Prompt example.

Example 2

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

Explanation: Remove all invalid parens.

Hints

  1. Model object-style prompts as arrays or operation streams when needed.
  2. Handle empty and boundary cases before the main logic.