Eight-Direction Keyboard Typing Time
Updated
Problem statement
Given a rectangular keyboard grid and a string word, return the minimum number of seconds needed to type word in order.
Every character appears once on the keyboard. Typing the first character costs no time. Each move from the current key to any of its eight neighboring cells, diagonals included, costs one second.
Function
typingTime(keyboard: String[], word: String) → intExamples
Example 1
keyboard = ["abc","efg","hil"]word = "bfgcil"return = 6A move between two keys costs the larger of their row gap and column gap. The moves b→f, f→g, g→c, c→i and i→l take one, one, one, two and one seconds, so the total is six.
Constraints
1 <= keyboard.length, keyboard[r].length <= 100.- All rows have the same length, and no character appears twice on the keyboard.
1 <= word.length <= 100000, and every character ofwordis on the keyboard.- Every character on the keyboard and in
wordis ASCII and takes up one grid cell.
More Benchling interview questions
- Bounded HTTP Retry FlowPHONE SCREEN · Seen Nov 2025
- Future-Aware Cache EvictionPHONE SCREEN · Seen Aug 2025
- Generate All Contiguous Protein ChainsPHONE SCREEN · Seen Jul 2024
- Generate Proteins from Contiguous Named SequencesPHONE SCREEN · Seen Aug 2021
- Next Palindrome NumberPHONE SCREEN · Seen Jul 2021
- Apply Overlapping HTML Formatting RangesPHONE SCREEN · Seen Feb 2019
- Validate Nested JSON Against a SchemaONSITE INTERVIEW · Seen Dec 2025
- Degenerate DNA Substring SearchONSITE INTERVIEW · Seen Jun 2024
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Jul 2021
- 2048 Move SimulatorOA · Seen Feb 2019
Similar questions at other companies
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Maximum Equal Parts for PrefixesAmazon · OA
- Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
- Closest ColorTwo Sigma · OATwo Sigma interview questions
- Most Cost-Effective MachineCapital One · ONSITE INTERVIEWCapital One interview questions
- Price Deviation AlertsJane Street · OAJane Street interview questions
- Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
- Logistic Regression with Gradient DescentYahoo · PHONE SCREENYahoo interview questions
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions