JavaScript Interview Questions

Closures, scope, hoisting, the JS object model, and more.

Learn all the "gotchas" to tricky JavaScript interview questions!

Try some questions now

You know how to code. Now learn how to interview.

In this free email course, you'll learn the right way of thinking for breaking down the tricky algorithmic JavaScript questions you'll be asked in your interviews.

JavaScript Technical Interview Questions

JavaScript Scope »

There's something tricky going on with scope in this JavaScript. Can you guess what will get logged to the console? keep reading »

What's Wrong with This JavaScript? »

There's a tricky bug in this JavaScript. Can you find it? keep reading »

Implement A Queue With Two Stacks »

Implement a queue with two stacks. Assume you already have a stack implementation. keep reading »

Compute the nth Fibonacci Number »

Computer the nth Fibonacci number. Careful--the recursion can quickly spin out of control! keep reading »

Rectangular Love »

Find the area of overlap between two rectangles. In the name of love. keep reading »

Making Change »

Write a function that will replace your role as a cashier and make everyone rich or something. keep reading »

Parenthesis Matching »

Write a function that finds the corresponding closing parenthesis given the position of an opening parenthesis in a string. keep reading »

Bracket Validator »

Write a super-simple JavaScript parser that can find bugs in your intern's code. keep reading »

Balanced Binary Tree »

Write a function to see if a binary tree is 'superbalanced'--a new tree property we just made up. keep reading »

Binary Search Tree Checker »

Write a function to check that a binary tree is a valid binary search tree. keep reading »

2nd Largest Item in a Binary Search Tree »

Find the second largest element in a binary search tree. keep reading »

MillionGazillion »

I'm making a new search engine called MillionGazillion(tm), and I need help figuring out what data structures to use. keep reading »

The Cake Thief »

You've hit the mother lode: the cake vault of the Queen of England. Figure out how much of each cake to carry out to maximize profit. keep reading »

Word Cloud Data »

You're building a word cloud. Write a function to figure out how many times each word appears so we know how big to make each word in the cloud. keep reading »

Largest Stack »

You've implemented a Stack class, but you want to access the largest element in your stack from time to time. Write an augmented LargestStack class. keep reading »

The Stolen Breakfast Drone »

In a beautiful Amazon utopia where breakfast is delivered by drones, one drone has gone missing. Write a function to figure out which one is missing. keep reading »

Delete Node »

Write a function to delete a node from a linked list. Turns out you can do it in constant time! keep reading »

Reverse A Linked List »

Write a function to reverse a linked list in place. keep reading »

Kth to Last Node in a Singly-Linked List »

Find the kth to last node in a singly-linked list. We'll start with a simple solution and move on to some clever tricks. keep reading »

Reverse String in Place »

Write a function to reverse a string in place. keep reading »

Reverse Words »

Write a function to reverse the word order of a string, in place. It's to decipher a supersecret message and head off a heist. keep reading »

Top Scores »

Efficiently sort numbers in an array, where each number is below a certain maximum. keep reading »

Which Appears Twice »

Find the repeat number in an array of numbers. Optimize for runtime. keep reading »

Find in Ordered Set »

Given an array of numbers in sorted order, how quickly could we check if a given number is present in the array? keep reading »

Find Rotation Point »

I wanted to learn some big words to make people think I'm smart, but I messed up. Write a function to help untangle the mess I made. keep reading »

Inflight Entertainment »

Writing a simple recommendation algorithm that helps people choose which movies to watch during flights keep reading »

Permutation Palindrome »

Check if any permutation of an input string is a palindrome. keep reading »

Recursive String Permutations »

Write a recursive function of generating all permutations of an input string. keep reading »

In-Place Shuffle »

Do an in-place shuffle on an array of numbers. It's trickier than you might think! keep reading »

Cafe Order Checker »

Write a function to tell us if cafe customer orders are served in the same order they're paid for. keep reading »

Simulate 5-sided die »

Given a 7-sided die, make a 5-sided die. keep reading »

Simulate 7-sided die »

Given a 5-sided die, make a 7-sided die. keep reading »

Two Egg Problem »

A building has 100 floors. Figure out the highest floor an egg can be dropped from without breaking. keep reading »

Find Repeat, Space Edition »

Figure out which number is repeated. But here's the catch: optimize for space. keep reading »

Find Repeat, Space Edition BEAST MODE »

Figure out which number is repeated. But here's the catch: do it in linear time and constant space! keep reading »

Find Duplicate Files »

Your friend copied a bunch of your files and put them in random places around your hard drive. Write a function to undo the damage. keep reading »

Apple Stocks »

Figure out the optimal buy and sell time for a given stock, given its prices yesterday. keep reading »

Product of All Other Numbers »

For each number in an array, find the product of all the other numbers. You can do it faster than you'd think! keep reading »

Highest Product of 3 »

Find the highest possible product that you can get by multiplying any 3 numbers from an input array. keep reading »

Merging Meeting Times »

Write a function for merging meeting times given everyone's schedules. It's an enterprise end-to-end scheduling solution, dog. keep reading »

Temperature Tracker »

Write code to continually track the max, min, mean, and mode as new numbers are inserted into a tracker class. keep reading »

Ticket Sales Site »

Design a ticket sales site, like Ticketmaster keep reading »

If we execute this Javascript, what will the browser's console show?

var text = 'outside'; function logIt(){ console.log(text); var text = 'inside'; }; logIt();

It's not "outside".

It's not "inside".

The script won't throw an error!

The console will log undefined.

To understand this, we need to explain a few things about Javascript.

Function-level scope. Functions create new scopes in Javascript:

function setVar(){ // inside this function we have a new scope // so this variable, declared in this function's scope, won't be available outside the function var varInFunction = 'inside a function'; } setVar(); console.log(varInFunction); // throws 'ReferenceError: varInFunction is not defined'

Blocks like if statements and for loops do not create a new scope (this is also true of Python and recent versions of Ruby, but untrue of Java and C):

if (true) { // this if statement doesn't create a new scope // so varInIf is available in the global scope var varInIf = 'inside an if statement'; } console.log(varInIf); // logs 'inside an if statement'

Declaration vs. assignment. A variable declaration simply tells the interpreter that a variable exists. By default it initializes the variable to undefined:

var unicorn; console.log(unicorn); // logs undefined (NOT a ReferenceError)

A variable assignment assigns a value to the variable:

unicorn = 'Sparkles McGiggleton';

We can both declare and assign in the same line:

var centaur = 'Horsey McPersonhead';

Hoisting. In Javascript, variable declarations are "hoisted" to the top of the current scope. Variable assignments, however, are not.

So returning to the original problem:

var text = 'outside'; function logIt(){ console.log(text); var text = 'inside'; }; logIt();

The declaration (but not the assignment) of text gets hoisted to the top of logIt. So our code gets interpreted as though it were:

var text = 'outside'; function logIt(){ var text; console.log(text); text = 'inside'; }; logIt();

So we have a new variable text inside of logIt that is initialized to undefined, which is what it holds when we hit our log statement.

Remember: when you declare a variable in JavaScript (using "var"), that variable declaration is "hoisted" to the top of the current scope—meaning the top of the current function or the top of the script if the variable isn't in a function.

Hoisting can cause unexpected behavior, so a good way to keep things clear is to always declare your variables at the top of the scope.

I just wanted to say thank you -- I got an offer from Google for their Engineering Residency program in NYC. I'm super excited. Interview Cake definitely helped me prepare for the Javascript questions they had for me. Keep up the great work! — A happy Interview Cake user (December 2016)
. . .