Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Solution: Next Letter

Problem Statement

Given an array of lowercase letters sorted in ascending order, find the smallest letter in the given array greater than a given key.

Assume the given array is a circular list, which means that the last letter is assumed to be connected with the first letter. Put simply: if the key is greater than or equal to the last letter of the array, the search wraps around, and the answer is the first letter of the array.

Write a function to return the next letter of the given key.

Example 1:

Input: ['a', 'c', 'f', 'h'], key = 'f'
Output: 'h'

.....

.....

.....

Like the course? Get enrolled and start learning!
Ankit Joshi

Ankit Joshi

· a year ago

Question is illogical, how can array be ascending order and last element is less than first element, we can just reword it that if no greater element found return first element.

Show 1 reply
Jimmy Hogerty

Jimmy Hogerty

· 2 years ago

title

Reading Progress

0%


Vote for new content