PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Bytedance

Solve Bracket Matching and Tree Width

Last updated: Jun 13, 2026

Quick Overview

This composite question evaluates proficiency with core data structures and algorithmic reasoning, covering string-based bracket validation (sequence matching and nesting correctness) and binary-tree level-span computation using position indexing and attention to numerical robustness.

  • hard
  • Bytedance
  • Coding & Algorithms
  • Backend Engineer

Solve Bracket Matching and Tree Width

Company: Bytedance

Role: Backend Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: HR Screen

Solve both coding problems below. ## Problem 1: Validate Balanced Brackets Given a string `s` containing only the characters `(`, `)`, `{`, `}`, `[` and `]`, determine whether the brackets are valid. A string is valid if: 1. Every opening bracket is closed by the same type of bracket. 2. Opening brackets are closed in the correct order. 3. Every closing bracket has a corresponding previous opening bracket. Return `true` if the string is valid; otherwise return `false`. ### Examples ```text Input: s = "()[]{}" Output: true ``` ```text Input: s = "([)]" Output: false ``` ```text Input: s = "{[]}" Output: true ``` ### Constraints - `0 <= s.length <= 100000` - `s` contains only bracket characters. --- ## Problem 2: Compute the Maximum Level Span in a Binary Tree You are given the root of a binary tree. For each depth level, imagine the tree is placed into a full binary tree layout where the root has position index `0`, a node at index `i` has left child index `2 * i` and right child index `2 * i + 1`. The span of a level is: ```text rightmost_position_index - leftmost_position_index + 1 ``` This span includes the gaps between existing nodes. Return the maximum span among all levels. ### Example ```text Input tree: 1 / \ 3 2 / \ 5 9 Position indexes by level: Level 0: 1 at index 0, span = 1 Level 1: 3 at index 0, 2 at index 1, span = 2 Level 2: 5 at index 0, 9 at index 3, span = 4 Output: 4 ``` ### Constraints - The number of nodes is between `0` and `100000`. - Node values are irrelevant. - Your solution should run in `O(n)` time, where `n` is the number of nodes. - Be careful to avoid integer overflow when assigning position indexes.

Quick Answer: This composite question evaluates proficiency with core data structures and algorithmic reasoning, covering string-based bracket validation (sequence matching and nesting correctness) and binary-tree level-span computation using position indexing and attention to numerical robustness.

Related Interview Questions

  • Reverse Nodes in K-Sized Groups - Bytedance
  • Reverse Linked List Groups - Bytedance (medium)
  • Solve Stack and String Shift Problems - Bytedance (medium)
  • Find LCA With Parent Pointers - Bytedance (medium)
  • Count Target-Sum Paths in an N-ary Tree - Bytedance (hard)
Bytedance logo
Bytedance
May 31, 2026, 12:00 AM
Backend Engineer
HR Screen
Coding & Algorithms
0
0
Practice Read

Solve both coding problems below.

Problem 1: Validate Balanced Brackets

Given a string s containing only the characters (, ), {, }, [ and ], determine whether the brackets are valid.

A string is valid if:

  1. Every opening bracket is closed by the same type of bracket.
  2. Opening brackets are closed in the correct order.
  3. Every closing bracket has a corresponding previous opening bracket.

Return true if the string is valid; otherwise return false.

Examples

Input: s = "()[]{}"
Output: true
Input: s = "([)]"
Output: false
Input: s = "{[]}"
Output: true

Constraints

  • 0 <= s.length <= 100000
  • s contains only bracket characters.

Problem 2: Compute the Maximum Level Span in a Binary Tree

You are given the root of a binary tree. For each depth level, imagine the tree is placed into a full binary tree layout where the root has position index 0, a node at index i has left child index 2 * i and right child index 2 * i + 1.

The span of a level is:

rightmost_position_index - leftmost_position_index + 1

This span includes the gaps between existing nodes. Return the maximum span among all levels.

Example

Input tree:
        1
       / \
      3   2
     /     \
    5       9

Position indexes by level:
Level 0: 1 at index 0, span = 1
Level 1: 3 at index 0, 2 at index 1, span = 2
Level 2: 5 at index 0, 9 at index 3, span = 4

Output: 4

Constraints

  • The number of nodes is between 0 and 100000 .
  • Node values are irrelevant.
  • Your solution should run in O(n) time, where n is the number of nodes.
  • Be careful to avoid integer overflow when assigning position indexes.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Bytedance•More Backend Engineer•Bytedance Backend Engineer•Bytedance Coding & Algorithms•Backend Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.