Find the Fewest Valid Single-Character Gene Mutations

Quick Overview

Practice finding a minimum mutation path between eight-character genes through a valid bank. Require exactly one changed position per step and handle unreachable or equal endpoints.

Find the Fewest Valid Single-Character Gene Mutations

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Find the Fewest Valid Single-Character Gene Mutations A gene is a string of eight characters from `A`, `C`, `G`, and `T`. One mutation changes exactly one character to a different character. Given `startGene`, `endGene`, and a list `bank` of valid genes, return the minimum number of mutations needed to reach `endGene`. Every gene entered after the starting point must occur in `bank`. The starting gene is valid even if absent from the bank. Return `-1` when no valid sequence reaches the target. ### Function `int minMutation(string startGene, string endGene, vector<string>& bank)` ### Example 1 ```text Input: startGene = "GGGGGGGG", endGene = "GGGGTTGG", bank = ["GGGGTGGG","GGGGTTGG"] Output: 2 ``` The listed intermediate changes position `4`, and the target then changes position `5`. The start and target differ at two positions, so fewer than two mutations cannot suffice. ### Example 2 ```text Input: startGene = "AAAAAAAA", endGene = "AAAAAATT", bank = ["AAAAAATT"] Output: -1 ``` The target differs at two positions, and no bank gene is available for the first single-character mutation. ### Constraints - `0 <= bank.length <= 10`. - Every input gene has length `8`. - All characters are from `A`, `C`, `G`, and `T`. ### Clarifications A step must change exactly one position; changing two at once is forbidden. If start and target already match, zero mutations are needed because the starting point is valid. Bank entries are valid destinations, not a prescribed order of visits. ### What a Strong Answer Covers - Single-character adjacency and bank membership after each mutation. - Minimum steps rather than merely finding some sequence. - An empty bank, equal endpoints, and an unreachable target. - Correctness, termination, and resource bounds.

Overview: Practice finding a minimum mutation path between eight-character genes through a valid bank. Require exactly one changed position per step and handle unreachable or equal endpoints.

Google logo
Google
Jul 11, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Find the Fewest Valid Single-Character Gene Mutations

A gene is a string of eight characters from A, C, G, and T. One mutation changes exactly one character to a different character.

Given startGene, endGene, and a list bank of valid genes, return the minimum number of mutations needed to reach endGene. Every gene entered after the starting point must occur in bank. The starting gene is valid even if absent from the bank.

Return -1 when no valid sequence reaches the target.

Function

int minMutation(string startGene, string endGene, vector<string>& bank)

Example 1

Input: startGene = "GGGGGGGG", endGene = "GGGGTTGG",
       bank = ["GGGGTGGG","GGGGTTGG"]
Output: 2

The listed intermediate changes position 4, and the target then changes position 5. The start and target differ at two positions, so fewer than two mutations cannot suffice.

Example 2

Input: startGene = "AAAAAAAA", endGene = "AAAAAATT",
       bank = ["AAAAAATT"]
Output: -1

The target differs at two positions, and no bank gene is available for the first single-character mutation.

Constraints

  • 0 <= bank.length <= 10 .
  • Every input gene has length 8 .
  • All characters are from A , C , G , and T .

Clarifications

A step must change exactly one position; changing two at once is forbidden. If start and target already match, zero mutations are needed because the starting point is valid. Bank entries are valid destinations, not a prescribed order of visits.

What a Strong Answer Covers Guidance

  • Single-character adjacency and bank membership after each mutation.
  • Minimum steps rather than merely finding some sequence.
  • An empty bank, equal endpoints, and an unreachable target.
  • Correctness, termination, and resource bounds.