Barbara
Barbara's Tech Blog

Barbara's Tech Blog

How to Solve a Coding Challenge

How to Solve a Coding Challenge

PREPare yourselves!

Barbara's photo
Barbara
·Mar 21, 2022·

5 min read

Solving a Challenge can be a... Challenge

When approaching a new coding challenge it can be all too easy to get overwhelmed, lost, flustered, or all of the above. Luckily these frustrations can be largely avoided by entering into each new challenge with a plan and framework for solving the problem. I'd like to share the approach I was taught and still follow when given a specific challenge to solve - be it a level 8kyu codewars kata or a high stakes whiteboard interview.

Enter PREP

Do as Scar says and be prepared

All you need to remember to keep your direction when approaching a coding challenge is the acronym PREP. It stands for:

P - Parameters

R - Returns

E - Examples

P - Pseudocode

By answering the following questions for each letter in PREP, you'll get a good feel for what the challenge is asking for, what you're being provided, what to look out for, and how to break it down to begin coding.

Parameters

What are they giving me? What are the expected arguments? Will it only ever be one type of data, or do I need to account for others? Will there ever be an invalid input? Do I need to account for unexpected edge cases?

Returns

What do they want me to return? Are they expecting a string? array? object? Is there a certain format the return is expected in? Will the expected return ever change for invalid inputs or edge cases?

Examples

Check your understanding! Are tests provided that you can reference as examples? What would happen in invalid or edge situations? Aim for at least 3 examples.

Pseudocode

In fairly plain speech, how would you solve this? What needs to happen to get from the starting inputs to the desired return? Think it through step by step, if you write something down and it is more than a single action, you may need to break it down more. Good pseudocode only comes with practice.

PREP - A practical example

Imagine we are presented with the following coding challenge:

Write a function to convert a given first and last name into initials. Only a first and last name separated by a single space will be provided, and capitalized initials should be returned separated by a single period. ex: Bob Vance => B.V

Please take a moment to do some PREP on this problem before continuing to read this article.

I can wait all day

Here are my quick PREP results:

  • P - string of 2 words with exactly 1 space between them
  • R - string of 2 capital letters with a period between them
  • E - Bob Vance => B.V , J Smith => J.S , bob bobbington => B.B
  • P - I need to get the first letter of each name, I need to insert a period between the initials, I need to capitalize the initials

The pseudocode is a little more on the plain English side than I would normally go, but I wanted to keep it very generic here as we are going to try 2 different coding solutions next.

Solution - Brute force (minimal methods) 🔨

Having knowledge of string methods would help here, but let’s proceed assuming you don’t:

  1. I know the first character of my return string will be the first initial, and I know that the input will never have extra characters before the first name, so I could start with an initial return value of name[0].
  2. Next, I'll need to add a period to the return value, changing it to name[0] + '.'
  3. I now need to find & tack on the last name initial. We'll need to locate that initial somehow, and since we're attempting this without the help of common string methods an easy way seems to be to loop through the string until we find the single provided space, and then grab the very next character. Add this to the return value.
  4. Finally, capitalize and return your return value

Here's what an example solution for this problem would look like in this situation:

function abbrevName(name){
  // Step 1 & 2 above
  let initials = name[0]+'.'
  // Step 3 above
  for (let i=0; i<name.length; i++) {
    if (name[i] == ' ') {
      // name[i+1] is the next character after our found space
      initials += name[i+1]
    }
  }
  // Step 4 above
  return initials.toUpperCase()
}

Despite what some code weenies may say, there is nothing wrong with this solution! It doesn't have a large time complexity, and it's easy to follow. It won't win many upvotes for 'Clever' solution on codewars, but who cares? Dare I say, it's actually coding language agnostic, too, which could be beneficial.

Solution - Gettin' fancy 💅

So you want to throw some methods around, huh? I get it. Depending on how wild you want to go with methods, there are a multitude of approaches for even a simple problem like this one. Here's how I would complete this example challenge with some methods:

  1. I know I'm being given a string with 2 words split with a space. Saving name.split(' ') as a new nameArr array separates first and last name, while simultaneously eliminating the space character.
  2. I only need to return the first letter of each value in my new array, joined with a ., so ${nameArr[0][0]}.${nameArr[1][0]}
  3. Don't forget to capitalize the return string!

Here's how this example approach would look:

function abbrevName(name){
  // Step 1 above
  let nameArr = name.split(' ')
  // Step 2 & 3 above
  return `${nameArr[0][0]}.${nameArr[1][0]}`.toUpperCase()
}

As I said before, this solution is just one of many, so don't come for me if it isn't your favorite approach!

Try it Out

Not every problem you encounter will need to be fully 'PREP'ed, but try your best to practice this approach as much as possible. With practice, it will become second nature.

As Moira says, one must PREPare

Have you tried it yet? If so, which part came easiest? Which part will take more practice to get used to?

 
Share this