Google Interview
String Manipulation
Coding Challenge
Algorithm Problems
Programming Interview

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.

Practice algorithms

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:

  1. Variable Weights: Assigning different costs to insertions, deletions, and substitutions, possibly even variable depending on position or character.
  2. String Transformation Rules: Introducing specific rules or transformations that are allowed or forbidden, either universally or conditionally based on the string's context.
  3. 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:

dp[i][j]=min{dp[i1][j1]+cost(s1[i],s2[j]),if s1[i]s2[j]dp[i1][j]+delCost(s1[i]),deletion costdp[i][j1]+insCost(s2[j]),insertion costdp[i][j] = \min\left\{ \begin{array}{ll} dp[i-1][j-1] + \text{cost}(s1[i], s2[j]), & \text{if } s1[i] \neq s2[j] \\ dp[i-1][j] + \text{delCost}(s1[i]), & \text{deletion cost} \\ dp[i][j-1] + \text{insCost}(s2[j]), & \text{insertion cost} \end{array} \right.

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.