Reported April 2022
Benchlinghash table

Eight-Direction Keyboard Typing Time

Reported by candidates from Benchling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Benchling OA. Under 2s to a working solution.
Founder's read

The Benchling OA reported in April 2022 looks like a grid problem, and that's the trap. You'll want to reach for BFS between every pair of keys. Don't. This is an eight-direction keyboard where each character appears once, and the cost between two keys is just the Chebyshev distance. Map each character to its (row, col), walk the word, and sum max(|dr|, |dc|). It's a short hash-table-plus-math problem. If you blank under the timer, StealthCoder runs invisibly during the live assessment and can hand you the pattern, but you should be able to write this one yourself in about ten minutes.

The problem

Every character appears once in a rectangular keyboard grid. Typing the first character costs no time. From the current key, each move to any of the eight neighboring cells costs one second.
Return the minimum number of seconds to type word in order.

Function
typingTime(keyboard: String[], word: String) → int

Examples
Example 1
keyboard = ["abc","efg","hil"]
word = "bfgcil"
return = 6
Chebyshev distances between consecutive keys sum to six.

Constraints
1 <= keyboard.length, keyboard[r].length <= 100.
Rows have equal length and all keyboard characters are unique.
1 <= word.length <= 100000, and every word character appears on the keyboard.
For this exercise, assume every keyboard and word character is ASCII; each character occupies one grid cell.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that diagonal moves cost the same as straight ones, so the shortest path between two cells on an empty grid is max(|r1-r2|, |c1-c2|). No search needed. Build a lookup from character to coordinates in one pass over the keyboard, then iterate through the word and add the distance from the previous key. Start with zero cost for the first character. The edge case that breaks a naive solution is word length up to 100000. Running BFS per step on a 100 by 100 grid will time out hard. Another pitfall is using Manhattan distance, which fails on diagonals. Repeated letters in the word are fine, since the distance is zero. Total work is O(grid size + word length). If your mind goes blank on the formula during the Benchling OA, StealthCoder is the safety net that surfaces it live, but the whole solution is about ten lines.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Eight-Direction Keyboard Typing Time cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as minimum time visiting all points. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Benchling's OA.

Benchling reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Eight-Direction Keyboard Typing Time FAQ

How hard is the Benchling keyboard typing problem really?+

Easy once you spot Chebyshev distance. The code is about ten lines. The difficulty is resisting the urge to run BFS or use Manhattan distance. If you see that eight-direction moves cost one each, the rest is a lookup and a sum.

What's the trick to this problem?+

Eight-direction movement with uniform cost means the distance between two cells is max(|dr|, |dc|). Precompute each character's row and column in a map, then sum that distance across consecutive characters in the word.

Why does BFS fail here?+

The word can be 100000 characters long. Running BFS on up to a 100 by 100 grid for each consecutive pair is far too slow. The closed-form Chebyshev distance is O(1) per pair, so the whole thing runs in linear time.

What edge cases should I test?+

Test a one-character word, which returns 0. Test repeated consecutive letters, which add 0. Test a single-cell keyboard. Test pure diagonal moves, where Manhattan would give double the right answer. Also check a one-row or one-column keyboard.

How do I prepare for this in 48 hours?+

Write this solution once from scratch with a hash map and the max formula. Then review similar grid-distance questions where you need to decide between BFS and a closed-form distance. Practice recognizing when movement rules make the path trivial.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Benchling.

OA at Benchling?
Invisible during screen share
Get it