0% found this document useful (0 votes)
56 views8 pages

JS (Interview Question)

majuy

Uploaded by

ma3325905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views8 pages

JS (Interview Question)

majuy

Uploaded by

ma3325905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FullStack.

Cafe - Kill Your Tech Interview

FullStack.Cafe - Kill Your Tech Interview

Q1: Explain equality in JavaScript ☆

Topics: JavaScript

Answer:

JavaScript has both strict and type–converting comparisons:

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

Some simple equalityrules:

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.

Q2: What is Scope in JavaScript? ☆

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.

Q3: What is typeof operator? ☆

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"

Q4: What is the object type? ☆

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
};

obj.a; // "hello world", accessed with doted notation


obj.b; // 42
obj.c; // true

obj["a"]; // "hello world", accessed with bracket notation


obj["b"]; // 42
obj["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";

obj[b]; // "hello world"


obj["b"]; // 42

Q5: Explain arrays in JavaScript ☆

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
];

arr[0]; // "hello world"


arr[1]; // 42
arr[2]; // true
arr.length; // 3

typeof arr; // "object"

Q6: Sum of Array Plus One ☆

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:

// ES5 method is nice and clean


exports.es5 = function (array) {
return array.reduce(function (memo, num) {
return memo + num;
}, array.length);
};

// Without array.reduce method isn't much different


exports.iterative = function (array) {
var result = array.length;

for (var i = 0; i < array.length; i++) {


result += array[i];
}

return result;
};

Q7: String Rotation ☆

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:

module.exports = function (a, b) {


return a.length === b.length && (a + a).indexOf(b) > -1;
};

Q8: Oddball sum ☆

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) {

// final count of all odd numbers added up


var final_count = 0;

// loop through entire list


for (var i = 0; i < nums.length; i++) {

// we divide by 2, and if there is a remainder then


// the number must be odd so we add it to final_count
if (nums[i] % 2 === 1) {
final_count += nums[i]
}

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;
});
}

Q9: Simple clock angle ☆

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) {

// we got 6 because 360/60 = 6


// 360 represents the full number of a degrees in a circle and
// 60 is the number of minutes on a clock, so dividing these two numbers
// gives us the number of degrees for one minute
return 6 * num;

simpleClockAngle(15);

Q10: Test divisors of three ☆

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.

function test_divisors(low, high) {

// we'll store all numbers and strings within an array


// instead of printing directly to the console
var output = [];

for (var i = low; i <= high; i++) {

// simply store the current number in the output array


output.push(i);

// check if the current number is evenly divisible by 3

Page 5 of 8
FullStack.Cafe - Kill Your Tech Interview

if (i % 3 === 0) { output.push('div3'); }

// return all numbers and strings


return output;

test_divisors(2, 10);

Q11: Sum of several arrays ☆

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;
}

sum_array([[3, 2], [1], [4, 12]]);

With reduce :

function sumArray(arr) {
return arr.reduce((t, e) => t.concat(e)).reduce((t, e) => t + e)
}

Q12: Lucky sevens ☆

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) {

// if less than 3 elements then this challenge is not possible


if (arr.length < 3) {
return "not possible";
}

// because we know there are at least 3 elements we can


// start the loop at the 3rd element in the array (i=2)
// and check it along with the two previous elements (i-1) and (i-2)
for (var i = 2; i < arr.length; i++) {
if (arr[i] + arr[i-1] + arr[i-2] === 7) {
return true;
}
}

// if loop is finished and no elements summed to 7


return false;

lucky_sevens([2, 1, 5, 1, 0]);

Q13: Explain how Bubble Sort works ☆

Topics: Sorting JavaScript

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:

Time Complexity: O(n^2) Space Complexity: O(n^2)

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;
}

for (let i = 0; i < pointer; i++) {


if (array[i] > array[i + 1]) {
let temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
// Recursive call on smaller portion of the array
return bubbleSort(array, pointer - 1);
};

PY

def bubbleSort(arr):
n = len(arr)

# Traverse through all array elements


for i in range(n):

# Last i elements are already in place


for j in range(0, n-i-1):

# traverse the array from 0 to n-i-1


# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]

Page 8 of 8

You might also like