cswork / libraryOpen practice workspace

Interview questions · Bloomberg

Solve two array and string problems

This two-part question evaluates combinatorial enumeration and subset-generation concepts for arrays alongside string parsing and parentheses-sequence validation for noisy input, measuring algorithm design, correctness, and complexity reasoning in array and string contexts.

You will solve two independent coding tasks.

Problem 1: Generate All Subsets of a Set

You are given an integer array nums containing n distinct elements.

Return all possible subsets (the power set) of nums.

  • A subset can be of any size from 0 to n.
  • The order of subsets in the output does not matter.
  • The order of elements inside a subset also does not matter.

Example

Input:

  • nums = [1, 2, 3]

One valid output (order may differ):

  • [
    [],
    [1],
    [2],
    [3],
    [1, 2],
    [1, 3],
    [2, 3],
    [1, 2, 3]
    ]
    

Constraints

  • 1 <= n <= 20
  • -10^9 <= nums[i] <= 10^9
  • All nums[i] are distinct.

You may return the result in any order.


Problem 2: Validate Parentheses in a Noisy String

You are given a string s that may contain:

  • the characters '(' and ')', and
  • any other characters (letters, digits, punctuation, etc.).

A string is considered parentheses-valid if, after ignoring all non-parenthesis characters, the remaining sequence of '(' and ')' is a valid parentheses sequence:

  • Every opening parenthesis '(' has a corresponding closing parenthesis ')'.
  • Parentheses are properly nested.

Implement a function that returns true if s is parentheses-valid, and false otherwise.

Examples

  1. Input: s = "a(b)c"

    • Ignoring non-parenthesis characters, we get "()" which is valid.
    • Output: true.
  2. Input: s = "(a(b)c"

    • Ignoring non-parenthesis characters, we get "(()" which is not valid.
    • Output: false.
  3. Input: s = "ab)c(d)e"

    • Ignoring non-parenthesis characters, we get ")()" which is not valid.
    • Output: false.

Constraints

  • 1 <= len(s) <= 10^5
  • s may contain any printable ASCII characters.

Community answers

Nothing here yet.

Join in on CSWork →