I saw this problem on HackerNews:

Most candidates cannot solve this interview problem:

* Input: "aaaabbbcca"
* Output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]

Write a function that converts the input to the output

I ask it in the screening interview and give it 25 minutes

How would you solve it?

Here's my solution(s) written in #Haskell: gist.github.com/hyperrealgophe

@hyperrealgopher
This is probably wrong I found a way to count characters in a string on stackoverrflow then adapted it for this

://gist.github.com/hyperrealgopher/19730725804ba8825d50d96f75b88a96

string="aaaabbbccd"
char="a"
echo "character count for a"
awk -F"${char}" '{print NF-1}' <<< "${string}"
char="b"
echo "charcter count for b"
awk -F"${char}" '{print NF-1}' <<< "${string}"
char="c"
echo "character count for c"
awk -F"${char}" '{print NF-1}' <<< "${string}"
char="d"
echo "character count for d"
awk -F"${char}" '{print NF-1}' <<< "${string}"

@zleap Close, but no cigar! Very interesting solution, but if you look at the output closer, you'll notice that 'a' at the end!

Follow

@hyperrealgopher

Good point and you want me to separate the count for the first set of a's and the last one not give a total.

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.