Companies like to ask you to solve whiteboard problems during coding interviews. If it’s an in-person interview, they’ll ask you to solve the problem on a physical whiteboard. If it’s a remote interview, they’ll either have you solve the problem on a website like HackerRank, or they’ll ask you the question in a “digital whiteboard” – including a plain old Google Doc, where they’ll watch you solve the problem. Some companies even use software that records your coding session.

These questions serve two purposes:

- They are weeder questions. The most popular weeder question of all time is FizzBuzz. Companies use questions like these to quickly screen out candidates.
- They allow the company to observe your problem solving process.

The best way to prepare for these interview questions is by doing a ton of practice whiteboard problems and developing your *problem solving process.* Don’t just jump into the code and rush for the answer. No, instead you want to walk the interviewer through your process.

In this article I’ll be simulating a coding interview by taking a whiteboard problem and walking through my process for solving it.

## Problem solving process

I’m going to solve the “Sock Merchant” HackerRank problem.

### 1 – Understand the problem statement

Here is the Sock Merchant problem statement directly from HackerRank.

The problem statements on HackerRank are like reading problems out of a math textbook. Honestly I find the problems easier to understand on CodeWars.

Nevertheless, the first thing I need to do is understand the problem statement. I like to restate the problem in my own words to clarify my understanding. For the Sock Merchant problem, I would restate this as the following:

`Given an array of sock colors, return the number of matching pairs of socks.`

### 2 – Write out the test cases

Instead of trying to jump straight to the solution, I write out a few test cases first.

```
Given 0 socks, return 0 pairs
Given 1 blue sock, return 0 pairs
Given 2 blue socks, return 1 pair
Given 4 blue socks, return 2 pairs
Given 2 blue socks, 1 red sock, return 1 pair
```

I always include invalid input cases, even if the problem states the input will never be invalid. In the real world, you can’t assume the input is always going to be valid. In this case, the problem stated that it will never have 0 socks, but I’m testing this case anyway.

### 3 – Solve the problem

Now that I understand the problem, and have written a few test cases, I can solve the problem on the whiteboard using pseudocode.

First I take care of the corner case where there are no socks.

`Given 0 socks, return 0 pairs`

For this I add a guard clause.

`if 0 socks, return 0`

Then I look at the rest of the test cases.

```
Given 1 blue sock, return 0 pairs
Given 2 blue socks, return 1 pair
Given 4 blue socks, return 2 pairs
Given 2 blue socks, 1 red sock, return 1 pair
```

It takes two socks of one color to make a pair. So I need to count how many socks there are of each color and divide by 2. Then sum all the pairs.

```
get counts of each color
divide the counts by 2
sum the results
```

Now I’ll walk through each test to verify my solution works. For brevity, I’ll walk through just one test case here.

```
test case: Given 2 blue socks, 1 red sock, return 1 pair
input = [blue, red, blue]
if 0 socks, return 0 //there's 3 socks, so continue
get counts of each color // blue = 2, red = 1
divide the counts by 2 //blue = 1, red = 0
sum the results // 1 + 0 = 1
```

### 4 – Write the real code

Now that I’ve solved the problem, I can translate the pseudocode into real code and submit it.

public static int SockMerchant(int n, int[] ar) { if(!ar.Any()) { return 0; } return ar.GroupBy(t => t) .Select(color => color.Count() / 2) .Sum(); }

## Where to find practice problems

By practicing a ton of problems, you’ll ace the coding interview.

For finding practice problems, I recommend HackerRank and CodeWars.

- HackerRank interview preparation kit: this is nice and structured, and exposes you to all the kinds of problems companies like to ask you to solve.
- CodeWars – most completed lvl 6-8 problems: there are tons of interesting problems here. It’s less structured than HackerRank and not curated. That’s why I always filter by
*Most Completed*.