Quiz 2
Registry Synced

Synthetic 6 · Longest Balanced Binary Span

128 words
1 min read
2026-08-02

Longest Balanced Binary Span

Write longest_balanced_span(bits). Return the longest contiguous slice containing an equal number of 0s and 1s. On equal lengths return the earliest slice. Return [] if none exists.

Template Code

python
def longest_balanced_span(bits):
    pass

Public Tests

is_equal(longest_balanced_span([0, 1, 1, 0, 1]), [0, 1, 1, 0]) is_equal(longest_balanced_span([1, 1, 0, 0]), [1, 1, 0, 0]) is_equal(longest_balanced_span([1, 1, 1]), [])
MCQ
3 Unit Assessment

Reference-only archive item

The completed export preserved the prompt as an image but not a reusable answer key. The reconstruction below is for study, not scoring.

Study reconstruction
def longest_balanced_span(bits):
    first = {0: -1}
    balance = 0
    best_start = 0
    best_length = 0
    for index, bit in enumerate(bits):
        balance += 1 if bit == 1 else -1
        if balance in first:
            start = first[balance] + 1
            length = index - first[balance]
            if length > best_length:
                best_start, best_length = start, length
        else:
            first[balance] = index
    return bits[best_start:best_start + best_length]
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.