Guest Post: Technical Interview Prep - Async Map

Technical Interview Preparation - Async Map

by Daniel Tsui

My talented colleague Daniel Tsui has started writing a series of guest posts about common technical interview questions, and it is my pleasure to host the first post, without further ado:

Hello! I'm Dan, before we get started, let me share what this post is all about. The aim is to help junior and intermediate software engineers preparing for technical interviews, I will do this by providing a prompt to practice writing code by hand (a crucial interview skill).

Readers are encouraged to follow allong, whiteboarding with a pen and legal pad, to pseudocode, to real code. No solution code will be provided here. Don't look up answers online until you've given it a solid shot!

Technical Context:



More (or less?) seriously, Async Map (or asyncMap) tests an applicant's understanding of asynchronous code execution and callback functions. In today's Node.js-heavy job market, it's a good idea to at least have some familiarity with serverside JavaScript concepts.

Here's the prompt:

"Write a function, asyncMap, that takes in an array of asynchronous functions (let's call them tasks) and a callback. asyncMap will invoke all of the functions and store their results in an array (i.e., the 'results array'). It will then invoke the callback on the results array once all of the asynchronous functions calls complete."

Example clarifying questions, with optional hints:

  • What exactly is a task?
    It's a good idea to get into the habit of re-explaining the problem to your interviewer, and a question like this will be good for that. Remember to supplement your explanation with visual aids using the whiteboard.

  • Does the results array need to hold results in a specific order?
    Sometimes the order will be specified in the prompt. Usually, the intention of this question is that the results should be returned in the order that they were called, and not the order that they get returned. When you look at the solution code, how would you refactor it in order to get the array ordered as the operations complete?

  • Edge cases: What happens if one of the functions doesn't complete? Do we need to perform error checking?
    A common application of Async Map is in server-side code. While you usually won't be asked to demonstrate node error-checking patterns, it's good to know what situations this toy problem is supposed to emulate.

Continuing the conversation:

Can you think of more example questions? It's great to keep your interviewer engaged in this area and almost never a good idea to jump straight into solving the problem without getting more detail on what you'd like to do.
Don't forget to ask the standard space and time constraints questions!

Common sources of confusion:

  • Saving in the correct order can be challenging. Hint: Can you somehow hold a pointer to the correct index a result is going to go?
  • It's easy to confuse the tasks themselves (that get invoked sequentially) with the final callback (that gets called once, on the results array). Make sure you separate those concerns out in your thought process, ideally with a diagram that demonstrates how those functions are different. The key here is to understand that we NEED to give the tasks themselves a callback function, to collate the results before invoking the final callback.
  • Forgetting to invoke the final callback itself, even if the results are being collated in the results array in the right order. How might you be able to tell when results array is ready to be passed to the callback (by the right number of results, perhaps)?

Example Test Case:

As mentioned above, we'd normally be using Async code and error checking. These timeouts simulate the effects of asynchronous function calls returning without the need for a server.

var wait500For9x9 = function(callback){  
    setTimeout(function () {
        callback(Math.pow(9,2));
    }, 500);
};
var wait300For5x5 = function(callback){  
    setTimeout(function(){
        callback(Math.pow(5,2));
    }, 300);
};
var wait100For1Plus1 = function(callback){  
    setTimeout(function(){
        callback(1+1);
    }, 100);
};

Example Starter Code:

asyncMap([wait500For9x9, wait300For5x5, wait100For1Plus1], function(results){  
      console.log(results);
      /*
      Should log [25, 81, 2], returning the results in the 
      order they entered the array, and not the order they returned
      Ie, disregarding the shorter timeout.
       */
    }); 

Get hacking, and good luck!

Still Stuck? Don't go to The Googles yet! Here's a visualization of the problem (and more hints)!

  • Think of the results array as a central holding point for all of your tasks.
  • Pay attention to the different callbacks and what they need to do. Draw them out!
  • Remember to invoke all of the functions being passed into asyncMap!

Daniel Tsui is a Software Engineer in SF, originally from Vancouver, Canada. He is currently working as a Hacker in Residence at Hack Reactor, an advanced software engineering immersive in SF. In past lives has he has been a professional poker player, student of statistics, and competitive Starcraft player.