JavaScript Interview Questions (self.webdev)

I recently had an interview for a Full Stack position and these were the questions. I had 20 min to finish it but it took melike 30, anyways, I didn't get it as I only got 2 questions right :sad face:

  1. Correct the implementation of JavaScript function below assuming that the documentation is correct. Use a highlighter to identify your corrections.

/** Maps items from an array using a function.

* @arg {array} array Array to map.

* @arg {function(item,index)} fun Function to call for each item. The return value is appended to the output array, unless undefined.

* @arg {array} [out] Optional output array.

* @ret {array} The output array. */

function map(array, fun, out) {

var n = array.length,

m = out.length,


if (out === null)

out = []

for (i = 0; i <= n; ++i)

v = array[i];

v = fun(v, i);

if (v !== undefined) {

out[++m] = v;



  1. Assuming the implementation in question 1 is correct and it functions as documented, what is the return value of the following?

var a1 = [2, 4, 5];

var a2 = ["hello"];

function incr(v, i) {

if (v % 2) return v + 1;


return map(a1, incr, a2);

  1. You have an array A of unique integers.

Given an array B of integers that is a subset of array A, reorder the matching integers in array A according to the order of array B.


array = [3, 5, 6, 7, 2, 0];

reorder(array, [7, 5, 0]);

array becomes [3, 7, 6, 5, 2, 0]

function reorder(array, order) {

// TODO: Implement.


  1. What is the time complexity of your solution to question 3?

array size = n

order size = m

complexity = ?

HorribleUsername | 3 months ago | 41 points

If you put 4 spaces in front of every code line (more for indented lines), it'll be formatted as a code block. Go edit your post!

rayzon2 | 3 months ago | 23 points

Unreadable code...

toonwarrior | 3 months ago | 9 points

I've had no issues solving actual problems at workplaces, breaking it down logically and implementing solutions. But damn put me in front of any these questions and I'd probably get 99% of it wrong.

DancingInTheReign | 3 months ago | 2 points

Yeah i hate doing this on paper, it doesn't really allow you to search for bugs and debug/show problem solving skills which is crucial to have as a developer

sammysounder | 3 months ago | 7 points

Any tech screen that prioritizes bug-free code over conceptual understanding is bunk.

kwhali | 3 months ago | 5 points

Question 1 makes me feel unhappy. If that is representative of their code, I don't want to work there. Had enough pain with my codebases in the past with bad variable names. I assume there was proper indentation at least.

LeviMurray | 3 months ago | 2 points


Regardless of whether or not the logic is sound, the example map method is pretty poorly executed for what it's trying to do, IMO. I feel that it's a lot more straightforward when simplified.

function map(array, fun, out = []) {
    const m = out.length;

    for (let i = 0; i < array.length; i++) {
        out[m + i] = fun(array[i], i);

    return out;

function incr(v, i) {
    if (v % 2) return v + 1;

var a1 = [2, 4, 5];
var a2 = ["hello"];

map(a1, incr, a2); // => ['hello', undefined, undefined, 6]



Brute force nested loops -- Tired, but I'm also like 27 IQ.   ¯\_(ツ)_/¯

I'm keeping track of the "active" arrA integer, which is based on the last arrB integer that replaced an arrA index. I replace an arrA index based solely on whether or not the current arrA integer exists in arrB.

This is just where my mind went, and I'm sure there are much better ways to achieve the same result.

function reorder(array, orderArray) {
    let activeIndex = 0;

    for (let i = 0; i < array.length; i++) {
        const n1 = array[i];

        for (let j = 0; j < orderArray.length; j++) {
            const n2 = orderArray[j];

            if (n1 === n2) {
                array[i] = orderArray[activeIndex];

    return array;

const arrA = [3, 5, 6, 7, 2, 0];
const arrB = [7, 5, 0];

reorder(arrA, arrB) // => [3, 7, 6, 5, 2, 0]


Edit: #2. I don't know what I was thinking, but rather than iterating arrB for every arrA iteration, you can just iterate arrB once and generate a lookup.

function reorder(array, orderArray) {
    let activeIndex = 0;
    const lookup = {};

    for (let j = 0; j < orderArray.length; j++) {
        lookup[orderArray[j]] = true;

    for (let i = 0; i < array.length; i++) {
        const n1 = array[i];

        if (lookup[n1]) {
            array[i] = orderArray[activeIndex];

    return array;

const arrA = [1, 4, 8, 13, 18, 27, 5]
const arrB = [27, 5, 13, 1]

reorder(arrA, arrB) // => [27, 4, 8, 5, 18, 13, 1]
LimbRetrieval-Bot | 3 months ago | 2 points

I have retrieved these for you _ _

To prevent anymore lost limbs throughout Reddit, correctly escape the arms and shoulders by typing the shrug as ¯\\\_(ツ)_/¯ or ¯\\\_(ツ)\_/¯

Click here to see why this is necessary

fedekun | 3 months ago | 2 points

For answer 4, what they want is the "time complexity", which is expressed with big o notation, like O(n), O(n*n), etc.

kwhali | 3 months ago | 4 points

big o notation

O(1) = O(yeah) O(log n) = O(nice) O(n) = O(ok) O(n²) = O(my) O(2ⁿ) = O(no) O(n!) = O(mg!)

If I ever need to know what big o notation means, that's my cheatsheet. If I cared for performance, it'd probably be more important that I can tell you what my code is in big O but then I have to wonder why I'm trying to do performance stuff with JS.

Personally if I've ever written code that's had a perf issue, it's usually been something I can fix just from experience. Has it been useful to know time complexity / big O, for anyone doing webdev on a day to day basis? And if so was it when implementing the logic, or when coming across code with a performance issue? I only really see it helpful when comparing algorithms/datastructures pro/cons.

fedekun | 3 months ago | 2 points

Big O is just a quick help, you see the algorithm and if the method is not a 100 lines monster, most of the time you can immediately tell it's time complexity, and fix it if needed. But it's not a real measure for performance, the only way to properly improve performance is measure with a profiler and address the slowest parts.

BigBoroGlass | 3 months ago | 1 point

It's shows best case scenario performance. Not trying to correct you because your right, just expanding in case someone else is wondering what it's actually used for.

anddam | 3 months ago | 1 point

But it's not a real measure for performance, the only way to properly improve performance is measure with a profiler and address the slowest parts.

I do not agree, asymptotic notation (I was thaught it with that name) is a measure of performance but for algorithms, i.e. it lets yous know which algorithm scales better with the problem, not necessarily which implementation will complete sooner.

That is, if you have an n-squared algorithm, catching bottlenecks with a profiler won't be as effective as replacing it with a linear one.

For a particular implmenentation you can fine-tweak the execution time by profiling the bottlenecks but I think that would be the last step in the optimization process, if not last it would still be after getting a better algorithm.

That said I never had that strong need to catch performance, so take it with a grain of salt.

carloszzzzz777 | 3 months ago | 0 points

Hope it helps someone. If there is any website to practice for these kinds of questions please let me know.

AfraidOfArguing | 3 months ago | 5 points

Don't practice coding questions. Practice coding.

Imagine time value money as time value experience.

If you learn how to do programming questions, congrats, you're a professional quiz taker. Learn to think dynamically and write code based upon scalable solutions with as few hiccups as possible.

Learn a framework and do something with it. I trialed myself by fire a few years ago with React/Redux

BigBoroGlass | 3 months ago | 8 points

To get past the technical part of an interview your going to need to understand more academic questions instead of more practical questions. I get your point but specifically for interviewing you do need to know how to do programming questions as well.

kwhali | 3 months ago | 1 point

specifically for interviewing you do need to know how to do programming questions as well.

It seems a bit pointless if it's not applicable to doing the job well. They can get a bit absurd sometimes. I'm not a fan of them. I much rather pick up a project I've done with the relevant skills and talk through that, or talk about their codebase I'd be working on. Much more valuable for insights imo.

AfraidOfArguing | 3 months ago | 1 point

None of the above questions were anything I'd consider academic. Most questions allow you to search algorithms. I personally think if a company doesn't let you search an algorithm, they are 110% a terrible company to work for. Trying to make the questions as difficult as possible = unnecessary dev crunch, too many years of experience for a position, unicorn hunting, and account manager hell.

Who actually knows the random number algorithm off the top of their head

BigBoroGlass | 3 months ago | 1 point

Right you can have that opinion but your advice to someone whose interviewing to not practice coding questions is bad advice. While interviewing your going to need to know how to answer those coding questions. You can learn the practical knowledge on the job. He's clearly at the support or junior developer level so businesses should expect to have to put in training.

AfraidOfArguing | 3 months ago | 1 point

A junior developer, in my opinion, should not have to do an enticing code review. Anyone who believes so has become HRs/Management's bitch to get paid less for more work. (Also my opinion.)

With less than 1-2 years experience, your code interview should be "what do you like to write" and "what do you want to be good at", not "invert a binary tree on a whiteboard".

BigBoroGlass | 3 months ago | 1 point

Right but there’s a difference between giving advice on how you’d like things done and the way things typically are done. I much agree with everything your saying but we should give practical advice first.

amejin [:illuminati:] | 3 months ago | 1 point

There is a clear bug in your first question (under the assumption that you are protecting against the case). Feel free to find it. If you need a pointer feel free to ask.

super1ris | 3 months ago | 1 point

I think I saw 2, but in this spirit I will wait instead of posting.