# Your Next Technical Interview Question – Seals & Penguins

###### Posted September 23, 2016 by

In this post, I’m going to walk you through a coding assignment I was tasked to perform during a technical interview for a new Dev Shop getting ready to launch inside the walls of San Quentin called REBOOTSQ. I was told that the interview would take about an hour and after a few preliminary questions like ‘Why I wanted to be a software engineer’ and ‘what do I love about coding’, my interviewer and I sat down in front of a computer to begin the challenge.

### Stage One:

I was asked to write a program for a game that takes a number as the argument and:

– if the number is divisible by five, returns ‘seals’

– if the number is divisible by seven, returns ‘penguins’

– if the number is divisible by five and seven, returns ‘penguins and seals’

– otherwise, just return the number

### Tip #1

Before writing any code, I would encourage you to write out the rules in pseudo-code in the text editor. This will ensure that you are solving the correct problem and help you to visualize the steps needed to arrive at your solution.

After I had the rules laid out, I realized I needed to write a function that takes a number as an argument and uses a conditional statement to determine the appropriate value to return. Let’s start there and sketch it out:

```
function penguinsAndSeals(num){
// code goes here
if( seal conditional and penguin conditional ){
return 'penguins and seals';
else if( seal conditional ){
return 'seals';
else if ( penguin conditional ){
return 'penguins';
else {
return num;
}
}
```

With this framework in place, the only thing left to do is figure out how to determine if a number is divisible by another number. I went straight for the modulus operator (%) that can be used to find the remainder of dividing one number into another. For example,

```
- 8 % 3; // returns 2
- 3 % 2; // returns 1
- 10 % 5; // returns 0
```

So, my conditional statement would look like (num % 5 === 0), which returns true or false depending on whether or not the number was divisible by five. I used the same logic for seven and combined the two to see if they were divisible by five and seven.

### Tip #2

It should be noted that to use this format, you must check for five and seven at the top of your if statement, because otherwise you might satisfy one of the other conditions first before finding out if it passed both the five and seven rule.

Here’s the finished product:

```
function penguinsAndSeals(num){
// code goes here
if( num % 5 === 0 && num % 7 === 0 ){
return 'penguins and seals';
else if( num % 5 === 0 ){
return 'seals';
else if ( num % 7 === 0 ){
return 'penguins';
else {
return num;
}
};
```

At this point, we ran a few test cases and got the following results.

```
penguinsAndSeals(10); // returns 'seals'
penguinsAndSeals(21); // returns 'penguins'
penguinsAndSeals(35); // returns 'penguins and seals'
penguinsAndSeals(16); // returns 16
```

So far, so good, but I soon realized that this challenge had only just begun. My interviewer then began to up the ante.

### Stage Two:

This time I was asked to write another Penguins and Seals program, but this time the conditions were as follows:

– if the number is divisible by five or has a five in in the number, return ‘seals’

– if the number is divisible by seven or has a seven in the number, returns ‘penguins’

– if the number is divisible by five or has a five in it and the number is divisible by seven or has a seven it, return ‘penguins and seals’

– otherwise, just return the number

As you can see, things were starting to get a little more complicated. I had already written the logic for testing whether the number is divisible by five or seven. Now, I had to figure out the code to see if number had a five or seven in it.

The quickest way I could think of was the turn the number into a string using either the native String() or String.toString() JavaScript methods and then use the indexOf() method to see if the string contained the number I was looking for. The String.indexOf() method attaches to a string and returns the index of the argument you pass in and if the string doesn’t have it, will return -1.

For example:

```
var num = 15; // 15 is assigned to the variable num
var stringNum = num.toString(); // changes num to “15”
stringNum.indexOf(5); // returns 1, because five is at the index of one
stringNum.indexOf(7); // returns -1, because there's no seven in stringNum
```

Now that I had code to determine if a number was divisible by five or seven and contained a five or seven, it was time to plug it into my if statement. One way would have been to include all of the logic into the if statement like so:

`if( num % 5 === 0 || stringNum.indexOf(5) > -1 ){ // run this code };`

This statement returns true if either the number is divisible by five, or if the string version of the number contains five and returns any number greater than -1. So, our entire function would look something like this.

```
function penguinsAndSeals(num){
// turn the number into a string
var stringNum = num.toString();
if( num % 5 === 0 || stringNum.indexOf(5) > -1 && num % 7 === 0 || stringNum.indexOf(7) > -1 ){
return 'penguins and seals';
else if( num % 5 === 0 || stringNum.indexOf(5) > -1 ){
return 'seals';
else if( num % 7 === 0 || stringNum.indexOf(7) > -1 ){
return 'penguins';
else {
return num;
}
};
```

### Tip #3

Functionally this code works, but I didn’t like the way the if statements were hard to understand, because they were all cluttered up with the rule logic. I decided to make things more readable by breaking out some of the logic and placing them into variables like so.

```
function penguinsAndSeals(num){
// turn the number into a string
var stringNum = num.toString();
var checkForSeals = num % 5 === 0 || stringNum.indexOf(5) > -1;
var checkForPenguins = num % 7 === 0 || stringNum.indexOf(7) > -1;
if( checkForSeals && checkForPenguins ){
return 'penguins and seals';
else if( checkForSeals ){
return 'seals';
else if( checkForPenguins ){
return 'penguins';
else {
return num;
}
};
```

Now, my checkForSeals and checkForPenguins variables would return true or false depending on whether the number was divisible by five or seven OR if the number as a string contained a five or seven.

Once again, we ran a few tests which all passed.

```
penguinsAndSeals(17); // returns 'penguins'
penguinsAndSeals(52); // returns 'seals'
penguinsAndSeals(70); // returns 'penguins and seals'
penguinsAndSeals(46); // returns 46
```

I had passed stage two and was feeling pretty good about myself. That was, until my interviewer began to describe the next challenge.

### Stage Three:

My interview told me that so far, we had been playing Penguins and Seals by a certain set of rules and that other people play this game with different rules. For example, Natrina plays using threes and fours instead of fives and sevens and Hans likes to play where the digits add up to five for seals and add up to seven for penguins. The task was to now write a program that allowed me to change the rules of the game depending on who was playing.

### Tip #4

It wasn’t immediately clear to me how to proceed, so I began asking questions. I would encourage anyone who gets in this type of situation to ask as many questions as you need to in order to fully understand the problem.

I have to be honest, at this point I was stuck. I started thinking about passing rules for seals and penguins as arguments, but I was having a difficult time of accounting for how many rules were going to be passed in and whether or not they were rules for seals, or rules for penguins. I won’t bore you with all the thoughts that were racing through my head, because actually I got saved by the bell. Our hour was up and now it was time for the feedback session.

My interview told me that while I hadn’t passed all three stages, I had done a fairly good job under pressure. He liked the way I had taken time to pseudo-code the rules of the games and to break the conditional logic into variables to make my code more readable.

As far as stage three goes, he suggested turning the rules into functions, store them in an array, and pass in an array of functions for seals and another for penguins as the game rules. He also suggested taking a more object oriented approach to creating the game. I heard the words that he was saying, but my mind was still a little grid-locked from stage three.

I was disappointed that I hadn’t aced the task, but I thanked my interviewer for his time and promised that I’d have the solution next time. I’m a firm believer that win or lose, attitude is everything.

### Later That Night:

I was in my cell later that night and pulled out a pencil and a notebook to sort out my stage three dilemma. I wrote out a list of my interviewer’s comments to help me make sense of things.

1. Turn my rules into functions

2. Store my functions in arrays

3. Pass in an array of functions for seals and penguins

4. Try an object-oriented approach

First things first. I saw how I could turn my rules into functions that would return true or false depending on the condition. For example,

```
function divisibleByFive(num){
return num % 5 === 0;
};
function containsSeven(num){
var numStr = num.toString();
return numStr.indexOf(7) > -1;
};
```

You can see how we can use this same format for all of rules we’ve used so far, plus any others we can think of like maybe Hans’s rule where the digits add up to 5.

```
function digitsAddUpToFive(num){
// turn the number into a string
var numStr = num.toString()
// split the digits into an array
var digitArr = numStr.split('');
// loop through the digit array and sum the numbers
var total = 0;
digitArr.forEach(function(digit){
total += digit;
}
return total === 5;
};
```

This step would now make my stage two function look like this:

```
function penguinsAndSeals(num){
var checkForSeals = divisibleByFive(num) || containsFive(num);
var checkForPenguins = divisibleBySeven(num) || containsSeven(num);
if( checkForSeals && checkForPenguins ){
return 'penguins and seals';
else if( checkForSeals ){
return 'seals';
else if( checkForPenguins ){
return 'penguins';
else {
return num;
}
};
```

As for step two, turning my rules into functions actually makes it easy to store them in arrays. Check it out,

```
var sealRulesArr= [divisibleByFive, containsFive];
var penguinRulesArr = [divisibleBySeven, containsSeven];
```

Now, we can write a function that allows us to pass these arrays in as arguments and loop through them to see if any of them return true. This is also an excellent opportunity to use the native Array.some() method that loops through an array and returns true if any of the elements pass a truth test.

So, here’s our code:

```
function penguinsAndSeals(num, rulesForSeals, rulesForPenguins){
var anySeals = rulesForSeals.some(function(rule){
return rule(num);
});
var anyPenguins = rulesForPenguins.some(function(rule){
return rule(num);
});
if( anySeals && anyPenguins ){
return 'penguins and seals';
else if( anySeals ){
return 'seals';
else if( anyPenguins ){
return 'penguins';
else {
return num;
}
};
```

Calling this function would look something like this:

```
penguinsAndSeals(20, sealRulesArr, penguinRulesArr); // returns 'seals'
penguinsAndSeals(27, sealRulesArr, penguinRulesArr); // returns 'penguins'
penguinsAndSeals(56, sealRulesArr, penguinRulesArr); // returns 'penguins and seals'
penguinsAndSeals(82, sealRulesArr, penguinRulesArr); // returns 82
```

Conclusion:

### Tip #5

I believe the essence of this technical interview was to see what I was going to do when the code got more and more complex. The lesson to be learned here is a good coder will find away to simplify the pieces and break the code into more modular and maintainable blocks.

Now, that we’ve figured out the stage three logic for a customizable Penguins and Seals game, I want to challenge you to use the object-oriented approach to come up with you own game. Here’s a little bit to get you started:

```
var PenguinsAndSeals = {
sealRulesArr: [],
penguinRulesArr: [],
playGame: function(num){
// code for game
}
};
```

This is object-oriented Penguins and Seals in its simplest form, but you could also write additional methods to add rules, display rules, clear rules, and whatever else you think will make the game more fun. After all, besides the fame, money, and glory…isn’t that what coding is all about?

by Chris Schuhmacher

The Last Mile Works Software Engineer