Search a Dictionary after Exactly One Character Replacement

Quick Overview

Search a Dictionary after Exactly One Character Replacement. Implement MagicDictionary(), buildDict(dictionary), and search(searchWord).

Search a Dictionary after Exactly One Character Replacement

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement MagicDictionary(), buildDict(dictionary), and search(searchWord). The dictionary is built once from distinct lowercase words. search returns whether changing exactly one character of searchWord can make it equal a dictionary word. Insertions and deletions are not allowed. ```cpp class MagicDictionary { public: MagicDictionary(); void buildDict(vector<string> dictionary); bool search(string searchWord); }; ``` ### Examples **Example 1:** ```text MagicDictionary(); buildDict(["cat","dog"]); search("cot") -> true; search("cat") -> false Explanation: A replacement is required, so an exact match alone does not qualify. ``` **Example 2:** ```text MagicDictionary(); buildDict(["map","mop"]); search("map") -> true; search("ma") -> false Explanation: map can change to mop; length cannot change. ``` ### Constraints - The dictionary contains 1..100 distinct words, each 1..100 English lowercase letters. Queries likewise contain 1..100 English lowercase letters. buildDict runs once before any query; search is called at most 100 times. ### Clarifying Questions Enforce exactly one substitution and distinguish an exact match from a one-edit match to another word. ```hint Read the boundary condition Which distance between equal-length words corresponds to exactly one replacement? ``` ### Contract considerations Enforce exactly one substitution and distinguish an exact match from a one-edit match to another word.

Overview: Search a Dictionary after Exactly One Character Replacement. Implement MagicDictionary(), buildDict(dictionary), and search(searchWord).

Bloomberg logo
Bloomberg
Jul 3, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement MagicDictionary(), buildDict(dictionary), and search(searchWord). The dictionary is built once from distinct lowercase words. search returns whether changing exactly one character of searchWord can make it equal a dictionary word. Insertions and deletions are not allowed.

class MagicDictionary {
public:
    MagicDictionary();
    void buildDict(vector<string> dictionary);
    bool search(string searchWord);
};

Examples

Example 1:

MagicDictionary(); buildDict(["cat","dog"]); search("cot") -> true; search("cat") -> false
Explanation: A replacement is required, so an exact match alone does not qualify.

Example 2:

MagicDictionary(); buildDict(["map","mop"]); search("map") -> true; search("ma") -> false
Explanation: map can change to mop; length cannot change.

Constraints

  • The dictionary contains 1..100 distinct words, each 1..100 English lowercase letters. Queries likewise contain 1..100 English lowercase letters. buildDict runs once before any query; search is called at most 100 times.

Clarifying Questions Guidance

Enforce exactly one substitution and distinguish an exact match from a one-edit match to another word.

Contract considerations

Enforce exactly one substitution and distinguish an exact match from a one-edit match to another word.