Let’s run through some interview style questions that are a bit different from the standard blackboard interview questions you might find on LeetCode or AlgoExpert. Predict-the-output questions show your interviewer your level of understanding of the language — a skill not tested by data structure and algorithms questions.
Below are four problems, their answers, and step-by-step explanations for each. You’ll get the most out of these problems if you try to solve them yourself before scrolling to the answer.
Take a look at the code below, what is logged to the console after the function call on line 8?
The console should read “undefined”, followed by something like “error: Uncaught ReferenceError: Cannot access ‘major’ before initialization”.
Let’s look at what is happening. We declared name with the var keyword, which allows hoisting. What this means for variables is that memory space is set aside for the variable before execution and by default is given the value of undefined, but there’s a catch — the value of “Elizabeth” isn’t assigned until line 4. Name still holds a value of undefined, so this is what is logged to the console.
Run through the code below, what is logged to the console on line 6?
If you guessed ‘33’ you’d be correct. The function uses the + operator to add together 3 values and return the result. As we have seen before it is important to remember order of execution. You may have seen the string parameter and thought the output is ‘123’ because numbers get concatenated to strings. The equation will operate from left to right, one parameter at a time. This means 1+2 is treated as a mathematical equation, resulting in 3. The…