MEDIUMasked at 1 company

Minimum Cost to Convert String I

A medium-tier problem at 58% community acceptance, tagged with Array, String, Graph. Reported in interviews at Atlassian and 0 others.

Founder's read

You're given two strings and a list of character conversion rules, each with a cost. Your job: find the minimum cost to transform one string into the other using those rules, or report if it's impossible. Atlassian has asked this. The trap is obvious: greedy character-by-character conversion fails because you can chain transformations (A to B costs 3, B to C costs 2, but A to C directly costs 6, so you route through B). This is a shortest-path problem hiding inside a string problem. If you freeze on the trick during your live assessment, StealthCoder surfaces the solution invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
58%

Companies that ask "Minimum Cost to Convert String I"

If this hits your live OA

Minimum Cost to Convert String I is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.

Get StealthCoder
What this means

The insight is that each conversion rule is a directed edge in a graph where nodes are characters and edge weights are costs. You need the shortest path from each source character to its target in the goal string. Run Dijkstra or Floyd-Warshall to precompute all shortest paths. Then iterate through the source string and sum the minimum cost to convert each character to its corresponding position in the target string. The gotcha: characters may not be directly reachable (impossible conversion), and the graph can have multiple intermediate hops that beat the direct route. Most candidates see the string indexing and miss the graph entirely. When you hit this live and the brute-force approach doesn't work, StealthCoder calculates the shortest paths and builds the answer in seconds, keeping your screen-share clean.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Minimum Cost to Convert String I recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Cost to Convert String I interview FAQ

Is this really asked at companies like Atlassian?+

Yes. Atlassian's data confirms it. Atlassian interviews often layer graph and string problems together to test whether candidates can recognize hidden structure. If you see a transformation-cost problem, assume it's a shortest-path problem underneath.

What's the actual trick I'm missing?+

You need to find the shortest path from each source character to its target, not just the direct rule. If you have A to B (cost 3) and B to C (cost 2) and need A to C, the shortest path is A to B to C (cost 5), not a nonexistent direct rule. Dijkstra or Floyd-Warshall precomputes this for all character pairs.

How do I know if a conversion is impossible?+

After computing all shortest paths, check if there's a valid path from each source character to its target. If any source character has no path to its target, return -1. An unreachable pair means the transformation fails entirely.

Which algorithm should I use: Dijkstra or Floyd-Warshall?+

Both work. Floyd-Warshall is simpler to code (triple nested loop) and runs in O(26^3) since you have at most 26 letters. Dijkstra from each character is faster asymptotically but more code. Given the small character set, Floyd-Warshall is the pragmatic choice.

How does this relate to the Array, String, Graph, and Shortest Path topics?+

The topics map cleanly: the input is an array of conversion rules (Array), you transform strings (String), you model rules as a directed graph (Graph), and solve using shortest-path algorithms (Shortest Path). It's a compressed version of all four concepts.

Want the actual problem statement? View "Minimum Cost to Convert String I" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.