Coding assignment for interview

Recently got a response from a recruiter which included a coding assignment. The assignment is similar to FizzBuzz and is trivial :

Optimized Math

This program should be a short piece of code that prints all of the positive integers from 1 to 100 as described more fully below. The program may contain multiple methods, and if using an OO language, should be contained within a single class or object. The program should be designed so that it begins execution when invoked through whichever mechanism is most common for the implementation language.

:black_small_square: Print out all positive integers from 1 to 100, inclusive and in order.

:black_small_square: Print messages to standard output, matching the Sample Output below.

:black_small_square: In the output, state whether the each integer is ‘odd’ or ‘even’ in the output.

:black_small_square: If the number is divisible by three, instead of stating that the number is odd or even, state that the number is ‘divisible by three’.

:black_small_square: If the number is divisible by both two and three, instead of saying that the number is odd, even or divisible by three; state that the number is ‘divisible by two and three’.

:black_small_square: Design the logic of the loop to be as efficient as possible, using the minimal number of operations to perform the required logic.

Sample Output

The number ‘1’ is odd.

The number ‘2’ is even.

The number ‘3’ is divisible by three.

The number ‘6’ is divisible by two and three

I completed the program easily but I just wanted a couple more sets of eyes to look at what I did before I submit it.

Here is my solution:

// Iterates from 1-100
for(var i = 1; i <= 100; i++) {
  // Checks if number is divisible by two and three
  if(i % 2 == 0 && i % 3 == 0) {
  	console.log('The number ' + i + ' is divisible by two and three');
  }
  // Checks if number is divisible by 3, but not 2
  else if(i % 3 == 0) {
  	console.log('The number ' + i + ' is divisible by three');
  }
  // Checks if number is odd, but not divisible by 2 or 3
	else if(i % 2 != 0) {
  	console.log('The number ' + i + ' is odd');
  }
  // Checks if number is even
  else {
  	console.log('The number ' + i + ' is even');
  }

}

I know this is a basic problem, but am I overlooking anything? The question mentions efficiency, should I being using a different approach like .map?

One thing I think you need to add is quotes around the numbers, like written in the example output

Map is an array method, so you would need an array with the numbers as starting point. Map also returns an array that has the elements of original array changed in some way. It doesn’t seem to be what they ask for. Using array and method you would use forEach as you need to execute a piece of code for each element of the array. But would it be more efficient?

I am sure you can find some service that tests how much time and memory your code needs

If you are doing something multiple times in a block or if you have computed conditions in your if block, you could store it in a local variable. Also, if you are returning a value or calling a function multiples times, such as console.log(), you can try to reduce it. This will help the code be more readable and maintainable. For the strings you are creating, you could use template literals instead of concatenating. They look cleaner, in my opinion.

....

const isDivisibleByTwo = i % 2 == 0;
const isDivisibleByThree = i % 3 == 0;
let output = "";

if (isDivisibleByTwo && isDivisibleByThree) {
   output = "....";
} else if (isDivisibleByThree) {
   output = "....";
} ....

....
console.log(output);
1 Like

wow
I can not believe they asked such basic questions. Every beginner could solve this without any problems. They should be embarrassed for asking such trivial stuff.

I even think it is slightly disrespectful because we tried to learn to programme, know the advanced stuff and then it isn´t even needed for the coding problem.

Thanks for sharing!

1 Like

I have been a developer for close to 20 years. I had an interview for a Sr. Java Engineer position a couple weeks ago, and I was asked to do a FizzBuzz type question on the whiteboard. I didn’t take offense; I just did it. It was a 4 hour interview overall and this was the first technical question. After 10 minutes on this easy one, we moved onto much harder ones. My approach to any level of whiteboard question is to show my thought process and communication skill.

And with this being a take-home question, I can only guess that they want to see how you organize your code, not just if you get the right answer or not or how fast and efficient your solution is. Things like descriptive variable names, let vs const, comments, unit tests, error checking, reusability, readability, etc.

3 Likes

Got a rejection email after submitting this to them. Zero feedback.

Made a Github repo and sent the HR person the link. 99% sure she doesn’t know what Github is and couldn’t figure out how to find the code and just sent me a rejection email.

You can still ask for feedback, they may answer you

When we did a recent intake of staff, we sent all applicants - regardless of whether they had a chance of getting a job or not - a link to an online test (not for a coding role) to complete as part of the decision process.

We did this simply because it was more efficient than first selecting a couple hundred appropriate candidates (and likely most of whom wouldn’t themselves be invited back for interviews anyway), and then making a separate email chain asking them to participate. I wonder if something similar may have happened here - particularly if they’ve had a raft of applications.

I’d chase it up with them ASAP whilst they’re still processing candidates.

Like you say, you may well have over-complicated matters in this instance by doing the right thing. Although I would be surprised if a recruiter in software engineering hadn’t come across GitHub.

As @ilenia says, press for some feedback. At the worst, you still don’t get hired by them and best, it was a genuine error on their side and they put you back into the pool of candidates.