Skip to content

Mock interview problem: "Multiply Three Numbers (with problem extensions)"

tim-hr edited this page Nov 24, 2016 · 1 revision

Setup

  • Make a Codestitch pad at codestitch.io.
    • If you don't have access privileges, ask Muhammad.
  • Paste the problem statement into the Codestitch pad.
  • Make sure they know that they can ask questions of the interviewer.

Problem statement

/*
Make a function that accepts three numbers and returns them multiplied together.

The function should take 3 parameters, not an array.
*/

Notes for interviewer

The point of this problem is obviously not the ridiculously-simple initial problem statement, it is the extensions below.

However, even with the dirt-simple initial problem, they should supply a couple of tests before proceeding.

Note that for our purposes in mock interviewing, to save time we can assume that they will be OK making full-on assertion functions. It's ok to use a console.log style of primitive assertion as a shorthand for doing the full thing.

E.g.,

console.log(multiplyThreeNumbers(2, 4, 8) === 64); // --> true

Problem extension #1: Reduce

It's likely that they will initially produce code like this:

function multiplyThreeNumbers(x, y, z) {
  return x * y * z;
}

So if they didn't reduce to begin with, then say something like:

Please modify your function to use reduce.

Note: If they try to modify the function to take an array instead of 3 separate parameters, tell them they should leave the parameters as they were.

Sample:

function multiplyThreeNumbers(x, y, z) {
  return [x, y, z].reduce(function(a, b) {
    return a * b;
  });
}

Or, if they choose to supply an initial value:

function multiplyThreeNumbers(x, y, z) {
  return [x, y, z].reduce(function(product, num) {
    return product * num;
  }, 1);
}

Make sure they keep running their tests.

Problem extension #2: Arrow functions

Now say something like:

Please modify the function to use an arrow function for the callback to reduce.

They may have encountered arrow functions before in passing, but they are pretty unlikely to have used them for real. Even more likely, they don't really know what arrow functions are at all.

Either way, give them a brief time window to consider your statement, possibly start to sketch something out, and hopefully very quickly volunteer to look up the details online.

If they start to flail or freeze, direct them to look up arrow functions on the MDN page.

The end result should look something like this:

function multiplyThreeNumbers(x, y, z) {
  return [x, y, z].reduce((a, b) => a * b);
}

Make sure they keep running their tests.

Problem extension #3: Rest parameters

Now say something like:

Please modify the function to use rest parameters.

They almost certainly will not have heard of "rest parameters".

They might vaguely associate it with "spread operator". Make sure they get basically that the two things are related but different.

Again give them a little think time before directing them further. Hopefully this time they are quick to look up rest parameters on MDN.

A good result:

function multiplyThreeNumbers(...numbers) {
  return numbers.reduce((a, b) => a * b);
}

Note: many people will mechanically dump theArgs from the example:

image

If they do that, remind them to apply all the Coding Style Guide stuff...

If they still don't get it, prompt them to fix the parameter name.

Testing with rest parameters

Make sure they keep running their tests.

Also, ask them what new functionality they get now that they have introduced rest parameters.

Answer: the function can now take any number of numbers, not just 3.

Have them refactor accordingly, with suitable tests.

Clone this wiki locally