PracHub
QuestionsLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/Anthropic

Debug Tokenization and Detokenization

Last updated: Jul 14, 2026

Quick Overview

Debug Python tokenization and detokenization functions while preserving their ID-only public interface. Repair iteration-order matching, reject unknown text instead of silently dropping it, validate duplicate IDs and empty tokens, and guarantee exact round trips for accepted input.

  • hard
  • Anthropic
  • Software Engineering Fundamentals
  • Software Engineer

Debug Tokenization and Detokenization

Company: Anthropic

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Onsite

# Debug Tokenization and Detokenization The source reports a debugging exercise with buggy tokenization and detokenization functions but does not include the exact code. The snippet and strict unknown-text policy below are a practice reconstruction. Preserve both public signatures and their ID-only return/input types. ```python def tokenize(text, vocabulary): out = [] i = 0 while i < len(text): for token, token_id in vocabulary.items(): if text.startswith(token, i): out.append(token_id) i += len(token) break else: i += 1 return out def detokenize(ids, vocabulary): inverse = {token_id: token for token, token_id in vocabulary.items()} return "".join(inverse[token_id] for token_id in ids) ``` Repair the functions under this contract: - `vocabulary` maps nonempty token strings to unique integer IDs. Reject an empty token or duplicate ID with `ValueError`. - `tokenize` uses longest-prefix matching at every position. Because dictionary keys are unique, no separate equal-length tie is possible. - The public stream contains IDs only. Therefore arbitrary unknown text cannot be reconstructed. Preserve the interface by raising `ValueError` at the first position with no matching token instead of dropping text or adding a payload type. - `detokenize` raises `ValueError` for an ID not present in the validated vocabulary. - Empty text maps to `[]`, and detokenizing `[]` returns `""`. For every text accepted by the longest-match tokenizer, `detokenize(tokenize(text, vocabulary), vocabulary)` must equal the original text. ### Clarifying Questions to Ask - Must the ID-only public interface remain unchanged? - Is exact round-trip behavior required for unknown input, or may unknown text be rejected? - Are empty tokens or duplicate IDs valid vocabulary data? - How large are the vocabulary and maximum token length? ### What a Strong Answer Covers - The iteration-order bug, silent unknown-character loss, and duplicate-ID inversion bug. - A deterministic repair that preserves `list[int]` and the original function signatures. - An explicit explanation of why exact arbitrary-unknown round trips are impossible with IDs alone. - Focused tests for overlapping tokens, unknown input, duplicate IDs, empty tokens, empty input, and unknown IDs. - Baseline and trie-based complexity. ### Follow-up Questions 1. What interface change would support lossless arbitrary unknown substrings? 2. How would a trie change construction cost and per-character work? 3. How would byte-level tokenization change the Unicode contract?

Quick Answer: Debug Python tokenization and detokenization functions while preserving their ID-only public interface. Repair iteration-order matching, reject unknown text instead of silently dropping it, validate duplicate IDs and empty tokens, and guarantee exact round trips for accepted input.

Related Interview Questions

  • Debug Python LRU Cache-Key Construction - Anthropic (hard)
  • Design a Parallel Image Processor - Anthropic (medium)
  • How do you review a design document? - Anthropic (hard)
  • Explain multithreading vs multiprocessing - Anthropic (medium)
|Home/Software Engineering Fundamentals/Anthropic

Debug Tokenization and Detokenization

Anthropic logo
Anthropic
Jul 8, 2026, 12:00 AM
hardSoftware EngineerOnsiteSoftware Engineering Fundamentals
11
0

Debug Tokenization and Detokenization

The source reports a debugging exercise with buggy tokenization and detokenization functions but does not include the exact code. The snippet and strict unknown-text policy below are a practice reconstruction. Preserve both public signatures and their ID-only return/input types.

def tokenize(text, vocabulary):
    out = []
    i = 0
    while i < len(text):
        for token, token_id in vocabulary.items():
            if text.startswith(token, i):
                out.append(token_id)
                i += len(token)
                break
        else:
            i += 1
    return out

def detokenize(ids, vocabulary):
    inverse = {token_id: token for token, token_id in vocabulary.items()}
    return "".join(inverse[token_id] for token_id in ids)

Repair the functions under this contract:

  • vocabulary maps nonempty token strings to unique integer IDs. Reject an empty token or duplicate ID with ValueError .
  • tokenize uses longest-prefix matching at every position. Because dictionary keys are unique, no separate equal-length tie is possible.
  • The public stream contains IDs only. Therefore arbitrary unknown text cannot be reconstructed. Preserve the interface by raising ValueError at the first position with no matching token instead of dropping text or adding a payload type.
  • detokenize raises ValueError for an ID not present in the validated vocabulary.
  • Empty text maps to [] , and detokenizing [] returns "" .

For every text accepted by the longest-match tokenizer, detokenize(tokenize(text, vocabulary), vocabulary) must equal the original text.

Clarifying Questions to Ask Guidance

  • Must the ID-only public interface remain unchanged?
  • Is exact round-trip behavior required for unknown input, or may unknown text be rejected?
  • Are empty tokens or duplicate IDs valid vocabulary data?
  • How large are the vocabulary and maximum token length?

What a Strong Answer Covers Guidance

  • The iteration-order bug, silent unknown-character loss, and duplicate-ID inversion bug.
  • A deterministic repair that preserves list[int] and the original function signatures.
  • An explicit explanation of why exact arbitrary-unknown round trips are impossible with IDs alone.
  • Focused tests for overlapping tokens, unknown input, duplicate IDs, empty tokens, empty input, and unknown IDs.
  • Baseline and trie-based complexity.

Follow-up Questions Guidance

  1. What interface change would support lossless arbitrary unknown substrings?
  2. How would a trie change construction cost and per-character work?
  3. How would byte-level tokenization change the Unicode contract?
Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More Anthropic•More Software Engineer•Anthropic Software Engineer•Anthropic Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.