Closures, scope, hoisting, the JS object model, and more.
Eric got the job at Google:
Glad I invested in your site—it clearly paid off immensely. You're offering a unique style of practice I couldn't find anywhere else. Keep doing what you're doing.
Lexi got the job at Facebook:
Interview Cake taught me how to approach new problems. The questions helped me feel confident and ready to crush my programming interviews.
Chris got the job at Palantir:
I used a number of resources to help prep for the coding interviews but Interview Cake stood out as by far and away the most useful. I owe you a massive debt of thanks.
Cody got the job at Amazon:
Your practice problems boosted my confidence and helped me to think critically throughout the process. And I got the job! Just wanted to say thanks.
Chris got the job at Apple:
I got the job! Your questions prepared me big time and I felt really relaxed throughout the entire process. I believe in Interview Cake!
Mark got the job at Google:
Your problems were great practice and were definitely the sort of problems that I saw in my interviews. Thanks!
Abhijeet got the job at Amazon:
Thanks again for everything, Parker. Interview Cake really prepared me to land the offer.
Zafir got the job at Google:
Especially if you're on a time crunch, Interview Cake is well worth investing in for those crucial few weeks before your big interview. Thanks Parker!
Zak got the job at Mixpanel:
I got offers from 7/8 of the companies at which I interviewed. After not going through a formal interview process in nearly a decade, your site really helped build my confidence. You’re a hero, Parker ;)
Eduardo got the job at Dropbox:
The problems are fun and very relevant, and the problem breakdowns are thorough enough to be part of a textbook on interviewing. Thanks, Parker!
Implement a queue with two stacks. Assume you already have a stack implementation. keep reading »
Computer the nth fibonacci number. Careful--the recursion can quickly spin out of control! keep reading »
Find the area of overlap between two rectangles. In the name of love. keep reading »
Write a function that will replace your role as a cashier and make everyone rich or something. keep reading »
Write a function that finds the corresponding closing parenthesis given the position of an opening parenthesis in a string. keep reading »
Write a function to see if a binary tree is 'superbalanced'--a new tree property we just made up. keep reading »
Write a function to check that a binary tree is a valid binary search tree. keep reading »
Find the second largest element in a binary search tree. keep reading »
I'm making a new search engine called MillionGazillion(tm), and I need help figuring out what data structures to use. keep reading »
You've hit the motherload: the cake vault of the Queen of England. Figure out how much of each cake to carry out to maximize profit. keep reading »
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 »
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 »
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 »
Write a function to delete a node from a linked list. Turns out you can do it in constant time! keep reading »
Write a function to reverse a linked list in-place. keep reading »
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 »
Write a function to reverse a string in-place. keep reading »
Write a function to reverse the word order of a string, in-place. It's to decipher a supersecret message and win the war. keep reading »
Efficiently sort numbers in an array, where each number is below a certain maximum. keep reading »
Find the repeat number in an array of numbers. Optimize for runtime. keep reading »
Given an array of numbers in sorted order, how quickly could we check if a given number is present in the array? keep reading »
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 »
Writing a simple recommendation algorithm that helps people choose which movies to watch during flights keep reading »
Check if any permutation of an input string is a palindrome. keep reading »
Write a recursive function of generating all permutations of an input string. keep reading »
Do an in-place shuffle on an array of numbers. It's trickier than you might think! keep reading »
Write a function to tell us if a deck of cards is a single riffle of two other halves. keep reading »
Given a 7-sided die, make a 5-sided die. keep reading »
Given a 5-sided die, make a 7-sided die. keep reading »
A building has 100 floors. Figure out the highest floor an egg can be dropped from without breaking. keep reading »
Figure out which number is repeated. But here's the catch: optimize for space. keep reading »
Figure out which number is repeated. But here's the catch: do it in linear time and constant space! keep reading »
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 »
Figure out the optimal buy and sell time for a given stock, given its prices yesterday. keep reading »
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 »
Find the highest possible product that you can get by multiplying any 3 numbers from an input array. keep reading »
Write a function for merging meeting times given everyone's schedules. It's an enterprise end-to-end scheduling solution, dog. keep reading »
Write code to continually track the max, min, mean, and mode as new numbers are inserted into a tracker class. keep reading »
Color the nodes in a graph so that adjacent nodes always have different colors. keep reading »
It's not "outside".
It's not "inside".
The script won't throw an error!
The console will log undefined.
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):
Declaration vs. assignment. A variable declaration simply tells the interpreter that a variable exists. By default it initializes the variable to undefined:
A variable assignment assigns a value to the variable:
We can both declare and assign in the same line:
So returning to the original problem:
The declaration (but not the assignment) of text gets hoisted to the top of logIt. So our code gets interpreted as though it were:
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.
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.