JS (Interview Question)
JS (Interview Question)
Topics: JavaScript
Answer:
Strict comparison (e.g., ===) checks for value equality without allowing coercion
Abstract comparison (e.g. ==) checks for value equality with coercion allowed
var a = "42";
var b = 42;
a == b; // true
a === b; // false
If either value (aka side) in a comparison could be the true or false value, avoid == and use === .
If either value in a comparison could be of these specific values ( 0 , "" , or [] -- empty array), avoid ==
and use === .
In all other cases, you're safe to use == . Not only is it safe, but in many cases it simplifies your code in a
way that improves readability.
Topics: JavaScript
Answer:
In JavaScript, each function gets its own scope. Scope is basically a collection of variables as well as the rules for
how those variables are accessed by name. Only code inside that function can access that function's scoped
variables.
A variable name has to be unique within the same scope. A scope can be nested inside another scope. If one
scope is nested inside another, code inside the innermost scope can access variables from either scope.
Topics: JavaScript
Answer:
JavaScript provides a typeof operator that can examine a value and tell you what type it is:
var a;
typeof a; // "undefined"
Page 1 of 8
FullStack.Cafe - Kill Your Tech Interview
a = "hello world";
typeof a; // "string"
a = 42;
typeof a; // "number"
a = true;
typeof a; // "boolean"
a = null;
typeof a; // "object" -- weird, bug
a = undefined;
typeof a; // "undefined"
a = { b: "c" };
typeof a; // "object"
Topics: JavaScript
Answer:
The object type refers to a compound value where you can set properties (named locations) that each hold
their own values of any type.
var obj = {
a: "hello world", // property
b: 42,
c: true
};
Bracket notation is also useful if you want to access a property/key but the name is stored in another variable,
such as:
var obj = {
a: "hello world",
b: 42
};
var b = "a";
Topics: JavaScript
Page 2 of 8
FullStack.Cafe - Kill Your Tech Interview
Answer:
An array is an object that holds values (of any type) not particularly in named properties/keys, but rather in
numerically indexed positions:
var arr = [
"hello world",
42,
true
];
Topics: JavaScript
Problem:
Write a function that takes an array of integers and returns the sum of the integers after adding 1 to each.
Solution:
return result;
};
Topics: JavaScript
Problem:
Find out if a string is a rotation of another string. E.g. ABCD is a rotation of BCDA but not ACBD .
Solution:
Page 3 of 8
FullStack.Cafe - Kill Your Tech Interview
First make sure a and b are of the same length. Then check to see if b is a substring of a concatenated with
a:
Topics: JavaScript
Problem:
Write a function called oddball_sum which takes in a list of numbers and returns the sum of all the odd elements.
Try to solve with and without reduce function.
Solution:
To solve this challenge we'll simply loop through the array while maintaining a final count, and every time an odd
number is encountered we'll add it to the count.
Without reduce :
function oddball_sum(nums) {
return final_count;
oddball_sum([1, 2, 3, 4, 5]);
With reduce :
function oddball_sum(nums) {
return nums.reduce(function(total, item){
if (item % 2 === 1) {
return total += item;
}
return total;
});
}
Page 4 of 8
FullStack.Cafe - Kill Your Tech Interview
Topics: JavaScript
Problem:
You will be given a number N that represents where the minute hand currently is on a clock. Your program
should return the angle that is formed by the minute hand and the 12 o'clock mark on the clock.
Solution:
If the input is 15 then your program should return 90 because a 90 -degree angle is formed by the minute hand
and the 12 o'clock mark on the clock. We'll solve this challenge by first calculating what angle is created by
each minute passing on a clock. Once we calculate this number, we multiply it by the input to determine the final
angle.
A method to solve such problems is to consider the rate of change of the angle in degrees per minute. The hour
hand of a normal 12-hour analogue clock turns 360° in 12 hours ( 720 minutes) or 0.5° per minute. The
minute hand rotates through 360° in 60 minutes or 6° per minute.
function simpleClockAngle(num) {
simpleClockAngle(15);
Topics: JavaScript
Problem:
You will be given 2 parameters: a low and high number. Your goal is to print all numbers between low and high,
and for each of these numbers print whether or not the number is divisible by 3. If the number is divisible by 3,
print the word "div3" directly after the number.
Solution:
We'll solve this problem by first creating a loop that will print each number from low to high. Once we have the
code for that written, we'll add a conditional that will check if the number is evenly divisible by 3 by using the
mod operator.
Page 5 of 8
FullStack.Cafe - Kill Your Tech Interview
if (i % 3 === 0) { output.push('div3'); }
test_divisors(2, 10);
Topics: JavaScript
Problem:
You will be given an array of several arrays that each contain integers and your goal is to write a function that
will sum up all the numbers in all the arrays. For example, if the input is [[3, 2], [1], [4, 12]] then your
program should output 22 because 3 + 2 + 1 + 4 + 12 = 22 . Solve without and with reduce .
Solution:
We will solve this challenge by looping through the entire array, and then looping through each inner array
adding up all the numbers.
function sum_array(arr) {
// store our final answer
var sum = 0;
// loop through entire array
for (var i = 0; i < arr.length; i++) {
// loop through each inner array
for (var j = 0; j < arr[i].length; j++) {
// add this number to the current final sum
sum += arr[i][j];
}
}
return sum;
}
With reduce :
function sumArray(arr) {
return arr.reduce((t, e) => t.concat(e)).reduce((t, e) => t + e)
}
Topics: JavaScript
Problem:
Write a function called lucky_sevens which takes an array of integers and returns true if any three consecutive
elements sum to 7.
Page 6 of 8
FullStack.Cafe - Kill Your Tech Interview
Solution:
To solve this challenge we'll simply loop through the array starting at the 3rd position, and checking if the
number at this index plus the two previous elements sums to 7. We continue doing this as we loop through the
entire array. Once we find three elements that sum to 7, we simply return true . If we reach the end of the array
without finding elements that sum to 7, we return false .
function lucky_sevens(arr) {
lucky_sevens([2, 1, 5, 1, 0]);
Answer:
Bubble Sort is based on the idea of repeatedly comparing pairs of adjacent elements and then swapping their
positions if they are in the wrong order. Bubble sort is a stable, in-place sort algorithm.
How it works:
In an unsorted array of n elements, start with the first two elements and sort them in ascending order.
(Compare the element to check which one is greater).
Compare the second and third element to check which one is greater, and sort them in ascending order.
Compare the third and fourth element to check which one is greater, and sort them in ascending order.
...
Repeat steps 1– n until no more swaps are required.
Visualisation:
Complexity Analysis:
Bubble sort has a worst-case and average complexity of O(n2) , where n is the number of items being sorted.
When the list is already sorted (best-case), the complexity of bubble sort is only O(n) . The space complexity for
Bubble Sort is O(1) , because only single additional memory space is required (for temp swap element).
Page 7 of 8
FullStack.Cafe - Kill Your Tech Interview
Implementation:
JS
// Normal
const bubbleSort = function(array) {
let swaps;
do {
swaps = false;
for (let i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
let temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
swaps = true;
}
}
} while (swaps);
return array;
};
// Recursively
const bubbleSort = function (array, pointer = array.length - 1) {
// Base Case
if (pointer === 0) {
return array;
}
PY
def bubbleSort(arr):
n = len(arr)
Page 8 of 8