Google Interview Find Crazy Distance Between Strings
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Google interview process is infamous for its rigor, often comprising questions designed not only to test your problem-solving abilities but also to assess your depth of knowledge in algorithms and data structures. One such problem that has captured the imagination of candidates is the "Find Crazy Distance Between Strings" problem. This problem extends the classical string distance measurements by introducing unique complexities, making it a great topic for discussion and exploration.
Understanding the Problem Statement
The aim of this problem is to determine a "crazy" distance between two strings. Unlike traditional string metrics such as Hamming or Levenshtein distance, this problem demands a nuanced approach that could involve a variety of algorithmic strategies. Before diving into the solutions, let's review some fundamental string metrics for context:
• Hamming Distance: Measures the number of positions at which the corresponding symbols are different. It is only applicable when strings are of equal length. • Levenshtein Distance: The minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another.
Problem Complexities
The term "crazy" implies that simple metrics won't suffice. Possible complexities include:
- Variable Weights: Assigning different costs to insertions, deletions, and substitutions, possibly even variable depending on position or character.
- String Transformation Rules: Introducing specific rules or transformations that are allowed or forbidden, either universally or conditionally based on the string's context.
- Unicode Characters: Handling extended character sets beyond ASCII, such as Unicode, adds additional layers of complexity with encoding considerations.
These complexities can be managed using advanced algorithms and data structures.
Solution Strategies
Dynamic Programming Approach
Dynamic programming provides a framework for tackling complex problems by breaking them down into simpler subproblems, which can be solved just once and stored for reuse.
Consider the following state representation for a dynamic programming approach to solve a complex string distance problem:
• Let dp[i][j]
represent the crazy distance between the prefix of the first i
characters of s1
and the first j
characters of s2
.
The recursive formula could look something like this:
This formula takes into account variable substitution, insertion, and deletion costs.
Example: Weighted Distance Calculation
Consider two strings, s1 = "kitten"
and s2 = "sitting"
. Define the operations with specific costs:
• Substitution:
• Cost is position-based, say 2 + position_diff
• Insertion/Deletion:
• Fixed cost, say 3
Let's calculate the crazy distance for this example using a table to store intermediate results:
• Segment Trees/Fenwick Trees: For transformations that have ranges, using these structures can speed up range queries or updates. • A Algorithm*: By representing the state space as a graph, where nodes are string prefixes and edges are edits with varying heuristics, A* could find an optimal path more efficiently in some cases.
Related reading
- Google Maps Given a point, how to find all points at a given road distance?
- Google similar images algorithm
- GPU based algorithm on AWS Lambda
- Gradient descent convergence How to decide convergence?
- Gradient Descent for Linear Regression Exploding
- Gradient Descent in Matlab
- gradient descent seems to fail
- Gradient Descent vs Adagrad vs Momentum in TensorFlow

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.