https://dzone.com/articles/my-favorite-coding-interview
Write a function that takes two parameters, a string and an integer. The function will return another string that is similar to the input string, but with certain characters removed. It's going to remove characters from consecutive runs of the same character, where the length of the run is greater than the input parameter.
Ex: "aaab", 2 => "aab"
Ex: "aabb", 1 => "ab"
Ex: "aabbaa", 1 => "aba"
Note: I'm evaluating your answer on the simplicity of your code. The goal is for it to be readable; someone new should be able to walk into this room afterwards and instantly understand what your function is doing.
Thought process. How do they get to a solution? Did they talk about their strategy?
It needs to work. If it doesn't, I will keep pushing them until it does.
Are the function and the variables well named?
Is the code as simple as possible, but not more simple?
Is the candidate struggling to remember standard library functions? Help them out.
Other Solutions
The most common variant answer I see is conflating the clauses that count the character and append to the output. Typically this results in a bug where the last character in a run is omitted.
Write a function that takes two parameters, a string and an integer. The function will return another string that is similar to the input string, but with certain characters removed. It's going to remove characters from consecutive runs of the same character, where the length of the run is greater than the input parameter.
Ex: "aaab", 2 => "aab"
Ex: "aabb", 1 => "ab"
Ex: "aabbaa", 1 => "aba"
Note: I'm evaluating your answer on the simplicity of your code. The goal is for it to be readable; someone new should be able to walk into this room afterwards and instantly understand what your function is doing.
def remove_extra_consecutive(input_str, max_consecutive_chars): output, prev_char, current_char_seen = '', None, 0 for current_char in input_str: if current_char == prev_char: current_char_seen += 1 else: current_char_seen = 0 prev_char = current_char if current_char_seen < max_consecutive_chars: output += current_char return outputWhile the candidate is writing code, I focus on the following.
Thought process. How do they get to a solution? Did they talk about their strategy?
It needs to work. If it doesn't, I will keep pushing them until it does.
Are the function and the variables well named?
Is the code as simple as possible, but not more simple?
Is the candidate struggling to remember standard library functions? Help them out.
Other Solutions
The most common variant answer I see is conflating the clauses that count the character and append to the output. Typically this results in a bug where the last character in a run is omitted.
def remove_extra_consecutive(input_str, max_consecutive_chars): output, prev_char, current_char_seen = '', None, 1 for current_char in input_str: if current_char == prev_char and current_char_seen < max_consecutive_chars: current_char_seen += 1 output += current_char else: if current_char != prev_char: current_char_seen = 1 output += current_char prev_char = current_char return outputAnother variant is using indexes instead of a named variable for the previous character. This can often lead to an index out of bounds bug. It's also common to forget to add the last character.
def remove_extra_consecutive(input_str, max_consecutive_chars): output, current_char_seen = '', 0 for i in range(len(input_str) - 1): if input_str[i] == input_str[i+1]: current_char_seen += 1 else: current_char_seen = 0 if current_char_seen < max_consecutive_chars: output += input_str[i] if current_char_seen <= max_consecutive_chars: output += input_str[i+1] return outputFinally some candidates try to alter the input string itself, or sometimes loop indexes, which can lead of off by one errors.
def remove_extra_consecutive(str, max_consecutive_chars): for i in range(len(str)): j = i + 1 while j < len(str): if str[i] != str[j]: break if j - i >= max_consecutive_chars: str = str[0:j] + str[j+1:] j += 1 return strI like this problem because it has one simple and robust solution, and a number of more complicated and brittle solutions. If a candidate gets through it quickly and correctly, I follow up by asking them about which edge cases they would want to create unit tests for. If it's in a dynamic language, I ask about how to make the function operate on any iterable.