Solving the classic “aaaabbbcca” coding challenge

Photo by @joshuaryanphoto on Unsplash.

I’ve seen many versions of this problem in coding interviews, so I decided to solve it here, just for fun.

The problem goes like this:

Input: “aaaabbbcca”
Output: [(“a”, 4), (“b”, 3), (“c”, 2), (“a”, 1)]
Write a function that converts the input to the output

One important thing to note here is that the input can have the same letter in different groups, making the solution a little bit tricky if, for example, you try to use dictionaries.

Here is my proposed solution:

def count_in_a_row(word)
counter = 1
result = Hash.new.compare_by_identity

for i in (1..word.length)
if word[i] == word[i-1]
counter += 1
else
result[word[i-1]] = counter
counter = 1
end
end

result
end

Pay attention to the line with Hash.new.compare_by_identity, that gives us the ability to use a hash with duplicated keys.

You can read the algorithm like this:

# Iterate the letters in the word, starting from the second one# # If the current letter is equal to the previous one, increase the counter# # If the current letter is different to the previous one,
# # save the key (the letter) with the value (the counter) in the hash,
# # and then reset the counter.
# Return the hash

But calling that function, the output would look a little bit different than required.

puts count_in_a_row("aaaabbbcca")Output:   {"a"=>4, "b"=>3, "c"=>2, "a"=>1}
Expected: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]

I would ask the interviewer if she is ok with the output or if the format is a hard requirement, and she wants me to implement it. In that case, I would do something like this:

def format_output(raw_output)
formatted_output = "["
raw_output.each do |key, value|
formatted_output += "(\"" + key + "\", " + value.to_s + "), "
end
formatted_output.delete_suffix!(", ")
formatted_output += "]"
end
puts format_output(count_in_a_row("aaaabbbcca"))Output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]
Expected: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]

You can find the source code of this post here.

--

--

--

I do Management and Infrastructure Engineering.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Updating Windows 10 and its problems…the old lyre…

CSS — Understanding the Basics 1

Running tests from maven command line: Different options

Everything as Code: Part 2 — Cloud infrastructure

Introduction to Arrogant Design Patterns

How to translate English to Russian Text in C# .NET Framework using Deep Learning AI

~*PDF $^EPub[READ] Coding Games in Scratch: A Step-by-Step Visual Guide to Building Your Own…

Modern C++: Implementing a Merkle Tree

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gepser Hoil

Gepser Hoil

I do Management and Infrastructure Engineering.

More from Medium

Mastering Recursion For Beginners-(Part 4)

Using an object instead of if / switch statements 😲?!

woman wearing white shirt standing inside library photo — Free Paris Image on Unsplash

Print all combinations of balanced parentheses

S.O.L.I.D PRINCIPLES IN PROGRAMMING