Job Application Code Challenge - a PHP solution
Tags: php, beginners, interview
Published: 2017-10-17 05:13:06

---
title: Job Application Code Challenge - a PHP solution
published: true
description: 
tags: php,beginners,interview
---

Earlier today I read [Ben Greenberg](https://dev.to/benhayehudi)'s [job application challenge](https://dev.to/benhayehudi/solving-a-job-application-code-challenge-30d) and thought it looked like a fun little challenge. I went to my "go to" language of PHP and knocked it out quick and noticed an error with the test string that was provided.

I didn't think to share my result until I saw another post by [Ryan Palo](https://dev.to/rpalo) who shared [his Ruby solution](https://dev.to/rpalo/secret-message-7do) to the problem.  So here is my PHP solution to the problem.

##The Challenge
The challenge is this (copied from the original post):

>Sort the characters in the following string:
abcdefghijklmnopqrstuvwxyz_
>
>by the number of times the character appears in the following text (descending):
>
>… [String omitted for brevity]
>
>Now take the sorted string, and drop all the characters after (and including) the _. The remaining word is the answer.

You can get the example string to run your code against [here](https://repl.it/MivX).

##My Solution
So first I saw a need to get an array with a count of all each character.  One of the nice things about PHP is that a string is an array of characters already so I didn't need to do any conversion of it.  To do this I just ran it through a for loop and went through the entire array and counted each one.

```php
<?php
$charCount = array();

for ($i=0; $i<strlen($longString); $i++) {
  if (!isset($charCount[$longString[$i]])) {
    $charCount[$longString[$i]] = 0;
  }
  $charCount[$longString[$i]]++;
}
```

Now that I have an array with the key being the character and the value containing the count, we need to sort the array.  This is done simply the arsort().

```php
<?php
arsort($charCount);
?>
```

Next we needed use implode() to concatenate all the items in the array into one string. Then it hit me that I needed to do this for the keys instead of the values of the array so I combined implode() with array_keys() to accomplish this.

```php
<?php
$sortedString = implode(array_keys($charCount));
?>
```

The last step was to drop everything including the "_" so I used explode() and, since this returns an array of strings, returned only the first string in that array which will be the word we are looking for.

##Final Solution
```php
<?php
$charCount = array();

for ($i=0; $i<strlen($longString); $i++) {
  if (!isset($charCount[$longString[$i]])) {
    $charCount[$longString[$i]] = 0;
  }
  $charCount[$longString[$i]]++;
}

arsort($charCount);
$sortedString = implode(array_keys($charCount));
$theWord = explode("_",$sortedString)[0];
?>
```

I can think of other ways (like using a reduce similar to Ben's JS solution) to solve this in PHP but this was the first method that hit me.  If you have a different version you like more please share it (and why) below cause I'd love to see it.


##Another Solution

The second I hit publish I thought "I bet there is a PHP function that I could use instead of the for loop that would make this even cleaner" and 5 seconds later I had it - array_count_values()

The one issue I ran in to though with attempting to use this function though was that it would treat $longString as a String and not an Array. To fix that I combined it with str_split() and now we have the simpler solution below.

```php
<?php
$charCount = array_count_values(str_split($longString));

arsort($charCount);
$sortedString = implode(array_keys($charCount));
$theWord = explode("_",$sortedString)[0];
?>
```

###Performance Increase
As with most cases when using a built in PHP function over writing your own, there is a performance increase for doing this.

I threw together a quick test script where I ran each version 1000 times and the second solution was just over twice as fast as the original.  So whenever possible, just use what PHP gives you.  :)