boltRomain Simon on SaaS & tech

8 Easy Programming Questions in Javascript (Spolsky's style)

tech

How not to spend you lifetime recruiting software developers? Follow Joel Spolsky's Easy Programming Questions advice! Here are some examples in Javascript

When I started interviewing software developers, I asked programming problems and I ended up spending a lot of time in interviews, spending 1 hour at least per interviewees. Then, I started to give them problems to solve at home or in a room, giving them some time to work on it, but the problem is that it does not reflect their real level. Everyone can copy-paste something from StackOverflow.

Then, I stumbled upon Joel Splosky's guide to interviewing software engineers and this changed the way I did technical interviews.

Easy Programming Question

He gives a plan for interviewing, one of them being "Easy Programming Question". The idea here is to ask really really easy interview questions that everyone can solve, but look at how fast they are solved.

You will notice there is a lot of variation in the time it takes for people to solve these. This will save you a lot of time, because if someone can't write these without even thinking about it, they will have a hard time doing more complicated stuff.

Good developers will take 30 seconds to solve these questions, and you can move forward with more elaborated interview questions.

And the good thing ? These simple questions work for everyone, from interships to senior developers.

Joel Spolsky gives 3 examples for these easy programming questions :

  1. Write a function that determines if a string starts with an upper-case letter A-Z
  2. Write a function that determines the area of a circle given the radius
  3. Add up all the values in an array

Very often, when I asked these questions, most of the people interviewed tried complicated things, when these can be solved with one liners. Here are my solutions, along with some things to look at :

1. Determine if a string starts with an upper-case letter A-Z

const isCapitalized = str => /[A-Z]/.test(str[0])
  • Do they know simple regexp?
  • Can they manipulate a string?

2. Determine the area of a circle given the radius

const circleArea = radius => Math.PI * radius * radius
  • Do they have simple math knowledge?
  • Do they know Math?

3. Add up all the values in an array

const sumArray = arr => arr.reduce((acc, val) => acc += val)
  • Do they use functional programming ? Do they know reduce?
  • Or prefer a for loop? If for loop, which one and why?

In both cases, ask if they know a different way to write this.

More short interview questions

These are the 3 original examples in Joel Spolsky's article, but we can think of many of these questions. Here are some additional ideas for short interview questions that might be solved with one line of Javascript :

4. Add multiple values to sorted array. It must remain sorted

const mergeArrays = (arr1, arr2) => [...arr1, ...arr2].sort()

5. Remove duplicates from an array

const removeDuplicates = arr => [...new Set(arr)]

6. Revert an object (key > value becomes value > key)

const revertObject = obj => Object.assign({}, ...Object.entries(obj)
  .map(([value, key]) => ({ [key]: value })))

7. Check if two strings are anagrams (contain the same letters)

const isAnagram = (a, b) => a.split('').sort().join('') === b.split('').sort().join('')

You can add a .toLowerCase() if you want

8. Find the largest number in an array

const findLargest = arr => arr.reduce((n, i) => (n > i) ? n : i, arr[0])

Readings

12 steps to better code

Related Articles