loading...
Cover image for Basic JavaScript Interview Questions

Basic JavaScript Interview Questions

dverybest profile image Charles Best Updated on ・3 min read

Hi everyone! So I had couple of interviews this year for JavaScript software development roles, and I felt its worth writing an article on some of the interview questions I was asked.
In this article I will write some of the questions I was asked and the answers to them.

Explain data structure

Data structure is a data organization, management, and storage format that enables efficient access and modification. A data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

Simply put, data structure is a defined format/way of storing and managing a collection of data.

List examples of data structures, explain and implement one

some common data structures includes:

  • array
  • linked list
  • double linked list
  • stack
  • queue
  • hash map
  • etc

I will explain and implement a stack.

A stack is a linear data structure that stores data in a LIFO(Last in First Out) manner, i.e. the item added/inserted last is the first item to be accessed. Data in a stack can only be accessed from one end(top of the stack).
A Stack data structure supports only two type of operation namely PUSH(insert/add item) and POP(delete/remove item) operation.

Implementation

class Stack {
    constructor() {
        this.top = -1;
        this.store = [];
    }

    pop = () =>{
        if(this.top<0){
            return null;
        }
        let poppedItem = this.store[this.top];
        this.store.length = --this.top+1;
        return poppedItem;
    }

    push = (item)=>{
        this.store[++this.top] = item;
        return;
    }

    getTop = ()=>{
        return this.store[this.top];
    }
}

let myStack = new Stack();

myStack.push("10");
myStack.push("34");
myStack.push("17");
console.log(myStack.getTop());//output 17
console.log(myStack.pop());
console.log(myStack.getTop());//output 34
Enter fullscreen mode Exit fullscreen mode

Explain Closure with code example

A closure is a function having access to the parent scope, even after the parent function has closed.
implementation

var add = (function(){
    let accumulator = 0;
    return function(value){
        return accumulator+=value;
    }
})();

console.log(add(3)); //output 3
console.log(add(5)); //output 8
console.log(add(7)); //output 15
Enter fullscreen mode Exit fullscreen mode

Closure makes it possible for functions to have private variable. E.g in the code above, the function returned by the anonymous function is still able to access the accumulator variable even though the anonymous function is done executing.

Explain Asynchronicity in JavaScript with code example

JavaScript is single threaded, meaning codes are executed sequentially/synchronously(line by line one after the other). Asynchronous JavaScript enables code execution without blocking the main thread, i.e code execute without blocking/stopping other code from executing immediately while its still running/executing.

code example

console.log("start");
new Promise((resolve,reject)=>{
    resolve({data:'hello world'});
}).then(res=>{
    console.log(res);
})
console.log("end");
//outputs
//start
//end
//{ data: 'hello world' }
Enter fullscreen mode Exit fullscreen mode

In the code above, console.log("end") executes before the promise even though the promise started executing first. This is because the promise is asynchronous and did not block the main thread allowing console.log("end") to execute while it is executing.

Explain Higher Order Functions.

Higher order functions are functions that take other functions as arguments and/or a function that returns a function.
code example

function logger(){
console.log("Hello world!");
}
setTimeOut(logger,2000);
Enter fullscreen mode Exit fullscreen mode

In the above setTimeOut is a higher other function that takes the function logger as an argument.

Conclusion

I hope you find this article useful and it helps you prepare for interview.
If you like the content, feel free to stay in touch, follow me on twitter

Discussion

pic
Editor guide
Collapse
recursivefaults profile image
Ryan Latta

One alternative to the stack example is to use shift/unshift.

A benefit is it covers adding and removing items, and allows you to treat the index of 0 as the top without worrying about it.

Also, not many people pay attention to those methods so you might even come across as clever for having non-obivous knowledge of JS Arrays.

Collapse
jperasmus profile image
JP Erasmus

Using shift/unshift for the stack would not be a very efficient solution when using JS arrays under the hood because that would turn an O(1) action into O(n) since every item in the array would have to update its index every time to perform either of these actions. Pushing onto the end or popping from it is O(1) since only a single item is affected.

Collapse
recursivefaults profile image
Ryan Latta

Being able to talk about that makes a better interview.

Collapse
ariesrobin profile image
Aries-Robin

Hi Charles!
My name is Serhii and I am from Ukraine.
I have just read your post carefully. I see you are very good at web developing.
As a beginner, I really want to learn from an excellent dev like you and make a career of mine.
If you are interest in my desire, please contact me, twentytwentyaugust@outlook.com
Thanks.

Collapse
grglldz profile image
Guiorgui

Closure is not necessarily a function, it can be any data type.
Closure is just a reference that is in the accessible scope, whose value comes from a scope that is not accessible.

Collapse
piotrlewandowski profile image
Piotr Lewandowski

"Closure is not necessarily a function, it can be any data type" not, exactly. According to MDN: "A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment)." (source: developer.mozilla.org/en-US/docs/W...)

Collapse
grglldz profile image
Guiorgui

That is not wrong. That's just one aspect of closures.

Thread Thread
piotrlewandowski profile image
Piotr Lewandowski

It's wrong when you phrase it like "JUST a reference that" ("just" suggests it's only that, not "one aspect of") when it's clearly not "just a" :) That's what I meant ;)

Collapse
aleaallee profile image
Alejandro esquivel

How common are queues, stacks, hash maps and linked lists in web development? I've never heard those terms in all my years learning and working as a web developer.

Collapse
drkvogel profile image
Chris Bird

They are things in computer science, sure, but perhaps not used in frontend development very much. Certainly "call stack" and "Memory Heap" referenced by the OP are things that exist, but are not really necessary to do frontend web development. Backend is a different matter...

Collapse
dverybest profile image
Charles Best Author

These may not be something you implement/use in your everyday web development, but understanding them can mean you have basic understanding of code execution in JavaScript. Things like the "call stack", "Memory Heap", "callback queue" and "event loop".

Collapse
aleaallee profile image
Alejandro esquivel

I've only heard about them when I was learning Java a couple years ago but forgot about those terms because I never had to think about them xD.

Collapse
sonnuk profile image
sonnuk

Good Job! Thank you for sharing
you explained concepts with examples, it's good.
Get Some more topics about javaScript:
tekslate.com/javascript-syntax-com...
tekslate.com/javascript-variables-...