JavaScript:
Interview Questions
& Challenges
30 questions and challenges you will
find in most interviews.
Welcome to "JavaScript: Interview Questions &
Challenges"!
This ebook is designed to help you prepare for
JavaScript job interviews, you will find a collection of
30 commonly asked interview questions and
challenges that will help you practice and master your
JavaScript skills.
These challenges and questions are designed to not
only test your knowledge but also to help you improve
your problem-solving skills.
Whether you are a beginner or an experienced
developer, this ebook is a valuable resource for
anyone who wants to excel in JavaScript interviews.
I’m confident that by working through the challenges
and questions in this book, you will gain confidence in
your skills and be better prepared to impress potential
employers.
So, if you are ready to take your JavaScript skills to
the next level and land your dream job, let's get
started!
01 Object keys VALUES
P ro b l e m :
D i s p l ay a l l t h e key s a n d va l u e s o f a n e s t e d o b j e c t
Solution:
B a s e c a s e : I f t h e t y p e o f a va l u e ( u n d e r a key ) i s
d i f fe re n t f ro m a n o b j e c t , t h e n w e p r i n t t h e key - va l u e
pair
I f t h e va l u e i s a n o b j e c t , c a l l t h e f u n c t i o n a g a i n w i t h
t h a t o b j e c t ( o b j [ key ] )
01 OBJECT KEYS VALUES
02 Variables Swap
Problem:
Swap 2 integers present in variables num1 and num2
without using temporary variable.
Solution:
It is possible with simple destructuring assignment using
array
Create an array with [num2, num1
Assign num1 to first position of newly created array,
num2 to second position, using array destructuring.
02 VARIABLES SWAP
03 stairs problem
Problem:
Create a function that given an integer it console logs a
step shaped stair with `n` levels using the # character.
e.g. steps(2) => # e.g. steps(3) => #
## ##
###
Solution:
Create an array with n length using Array.fro
Fill it with numbers from 0 to n with callback f
Map over the array and create another array
with # char repeated from 0 to n
Iterate over the array and console log the
results
03 STAIRS PROBLEM
04 Array check
Problem:
How do you check if a variable holds an array?
Solution:
You can use Array.isArray() method
04 ARRAY CHECK
05 age difference
Problem:
Given the following array of objects, find the difference in
age between the oldest and youngest family members,
and return their respective ages and the age difference.
05 AGE DIFFERENCE
05 age difference
Solution:
Save only the ages into an array. [13, 56, 45, 67
Math.min() and Math.max() functions accepts an array
of numbers, so create an array where the first position
is the minimum value of the array of ages and the
second position the max age
The last position is the difference between the
Math.min(...) and Math.max(...)
05 AGE DIFFERENCE
06 Flat array
Problem:
Given an array of arrays, flatten them into a single array.
Solution:
call reduce array helper method and have the
accumulator (flat) be an empty array that will always be
flat. toFlatten is the current item of the array
On each iteration we concatenate the flat array with the
result of calling flatten again if current item is an array,
otherwise concat that value
The base case for the recursive call will be when the
toFlatten value is not an array, in which case we just
add it to the array.
06 FLAT ARRAY
07 Palindrome
Problem:
A palindrome is a word, phrase, number, or other sequence of
characters which reads the same backward or forward.
Return true if the given string is a palindrome. Otherwise, return
false.
Solution:
Lowercase the string and use the RegExp to remove
unwanted characters from it
Use the same chaining methods with built-in functions
from the previous article 'Three Ways to Reverse a
String in JavaScript
Check if reverseStr is strictly equals to lowRegStr and
return a Boolean
07 palindrome
08 vowels
Problem:
Given a string of text containing 0 or more vowels, count
the number of vowels that can be found within the text.
Solution:
The match() method retrieves the result of matching a
string against a regular expression
We are looking for the vowels [aeiou]
`i` in a regular expression is meant to ignore the case,
so it will match both a and A for example
g stands for global, and it tells the match fn to perform
a global search.
08 vowels
09 NULL VS UNDEFINED
Problem:
What is the difference between null and undefined in
JavaScript?
Solution:
Undefined: means a variable has been declared but has
not yet been assigned a value
Null: Is an assignment value. It can be assigned to a
variable as a representation of no value.
09 null vs undefined
10 Reverse a string
Problem:
Given the string ‘XWZ’ return ‘ZWX’
Solution:
split(‘’) when applied to a string returns a new array
with each char of the string on a position of the array
like [‘X’, ‘W’, ‘Z’]
reverse() function when applied to an array it reverses
the string values like [‘Z’, ‘W’, ‘X’
join(‘’) returns a new string by concatenating all of the
elements in an array, separated by the specified
separator string like ZWX
10 reverse a stirng
11 fizz buzz
P ro b l e m :
Wr i t e a f u n c t i o n t h a t l o g s t h e n u m b e r s f ro m 1 to n , b u t
f o r m u l t i p l e s o f 3 p r i n t s “ fi z z ” a n d f o r m u l t i p l e s o f 5
p r i n t s “ b u z z ” . Fo r n u m b e r s t h a t a re m u l t i p l e s o f b o t h ( 3 &
5 ) p r i n t s “ fi z z b u z z ” .
11 fizz buzz
11 fizz buzz
P ro b l e m :
Wr i t e a f u n c t i o n t h a t l o g s t h e n u m b e r s f ro m 1 to n , b u t
f o r m u l t i p l e s o f 3 p r i n t s “ fi z z ” a n d f o r m u l t i p l e s o f 5
p r i n t s “ b u z z ” . Fo r n u m b e r s t h a t a re m u l t i p l e s o f b o t h ( 3 &
5 ) p r i n t s “ fi z z b u z z ” .
Solution:
I f t h e c u r re n t n u m b e r i s a m u l t i p l e o f b o t h 3 a n d 5 , i t
m e a n s t h a t i t h a s to b e m u l t i p l e o f 3 * 5 = 1 5 ( i % 1 5 = = = 0 ) ,
we print "FizzBuzz"
I f t h e c u r re n t n u m b e r i s o n l y a m u l t i p l e o f 3 ( i % 3 = = = 0 ) ,
we print "Fizz"
I f t h e c u r re n t n u m b e r i s o n l y a m u l t i p l e o f 5 ( i % 5 = = = 0 ) ,
we print
11 fizz buzz
12 var, let & const
Problem:
Please state the differences between var, let and const
in JavaScript.
is it block creates a global is it is it
scoped? variable? reassignable? redeclarable?
var
let
const
Subtle use cases to have in mind
var is function scoped when is declared inside a
functio
const is not reassignable but will accept changes in
values (for arrays and objects
let can be redeclared outside of the scope it was
declared.
12 var, let & const
13 Chunk problem
Problem:
Given an array and a chunk size, divide the array into
many subarrays with each subarray being of the chunk
size.
e.g. chunk([1,2,3,4,5], 2) -> [[1,2], [3,4], [5]]
length 2
13 chunk problem
13 Chunk problem
Problem:
Given an array and a chunk size, divide the array into
many subarrays with each subarray being of the chunk
size.
e.g. chunk([1,2,3,4,5], 2) -> [[1,2], [3,4], [5]]
length 2
Solution:
create an auxiliary array
create an auxiliary index
iterate the array from the 0 position
create the first chunk by slicing the array from the
index until the size parameter. (e.g. chunk = [1,2]
Increase the index by the size param.
e.g. index = 0 + 2 =
repeat the process with new index. This time the new
chunk will be arr.slice(2, 4) = [3,4]
13 chunk problem
14 Capitalize words
Problem:
Create a function that given a string, it capitalizes the
first letter of the words in a sentence.
e.g. capitalize(“hello world”) -> Hello World
Solution:
create an auxiliary words array and iterate through
each word in the string
capitalize each word by accessing its first letter and
calling the toUpperCase function
join the words on the auxiliary array with a space
between them.
14 capitalize words
15 reverse integers
Problem:
Create a function that given an integer, it returns an
integer that is the reverse ordering of numbers.
e.g. reverseIntegers(7364) -> 4637
reverseIntegers(-15) -> -51
reverseIntegers(-90) -> -9
Solution:
Convert the integer into a string and split the string so
that every number is in a position of the array
Reverse the array calling the reverser() helper method
Convert the array back to string and convert it into an
integer and multiply it by it’s original sign.
15 reverse integers
16 First non repeating
character.
Problem:
How could you find the first non repeating char in a
string?
e.g. firstNonRepeatingChar(“This is an example sentence”) -> h
16 first non repeating character
16 First non repeating
character.
Problem:
How could you find the first non repeating char in a
string?
e.g. firstNonRepeatingChar(“This is an example sentence”) -> h
Solution:
create a char auxiliary variable to store each charater
on the sentence.
create a charCount object to store a key value map of
each character and the amount of times it exists on the
sentence. Something like { ‘s’: 2, ‘j’: 1 ...
iterate through each letter on the sentence
store each character on a temp auxiliary variable
If that character doesn’t exist on the charCount object
we add it with an occurrence of 1, otherwise we just
increase its occurrence
Finally we iterate over the charCount object and return
the first key (character) which occurrence count is 1.
16 first non repeating character
17 highest occurrence
Problem:
Given an array, get the element with the highest
occurrence in an array.
e.g. highestOccurrence([1,2,’a’,4,3,’a’,’b’,’a’,6,1]) -> ‘a’
17 highest occurrence
17 highest occurrence
Problem:
Given an array, get the element with the highest
occurrence in an array.
e.g. highestOccurrence([1,2,’a’,4,3,’a’,’b’,’a’,6,1]) -> ‘a’
Solution:
if array has no items we return nul
create auxiliary variables for the amount of element
occurrences, the current max element, and the max
count an element has been repeated
iterate through the array and store each element on the
el variable
if our occurrence map object has already a count for
that element, we increase its value, otherwise we
initialize the element’s occurrence count with 1
if the current element’s occurrence count is higher than
the max count, we set the current element as the
element with maximum count, and we update our
maxCount variable with the element’s count
we return the max element after finishing the iteration
of the array.
17 highest occurrence
18 intersection
Problem:
Given two arrays of primitives, return the elements that
are included in both arrays (intersection).
e.g. intersection([1,2,3], [2,3,4,5]) -> [2, 3]
Solution:
filter all the elements from the first array that are not
included in the second array
for this we use the filter array helper, and the condition
is that the current value of the array has to be included
in the second array.
18 intersection
19 Make Pairs
Problem:
Write a method that given an object with key value pairs,
returns a deep array like [[key, value]]
e.g. makePairs({ a: 1, b: 2 }) -> [['a', 1], ['b', 2]]
Solution:
Object.keys returns an array with all the keys of the
object
we iterate over those, and for each key, we return an
array that has the key in the first position and the value
of that key (obj[key]) in the second position.
19 make pairs
20 Total Price
Problem:
Create a function that takes an array of objects
(groceries) which calculates the total price and returns it
as a number. A grocery object has a product, a quantity
and a price, for example:
e.g. getTotalPrice([ { product: "Milk", quantity: 3, price: 1.50 },
{ product: "Cereals", quantity: 2, price: 2.50 } ]) ➞ 9.5
Solution:
20 total price
20 Total Price
Problem:
Create a function that takes an array of objects
(groceries) which calculates the total price and returns it
as a number. A grocery object has a product, a quantity
and a price, for example:
e.g. getTotalPrice([ { product: "Milk", quantity: 3, price: 1.50 },
{ product: "Cereals", quantity: 2, price: 2.50 } ]) ➞ 9.5
Solution:
we use reduce array helper and create an accumulator
value that starts from 0.
On each iteration we increment the accumulator by the
current element price * current element quantity
we return the accumulator on each iteration
20 total price
21 positive dominant
P ro b l e m :
A n a r ray i s p o s i t i ve d o m i n a n t i f i t c o n t a i n s s t r i c t l y m o re
u n i q u e p o s i t i ve va l u e s t h a n u n i q u e n e g a t i ve va l u e s . Wr i t e
a f u n c t i o n t h a t re t u r n s t r u e i f a n a r ray i s p o s i t i ve
dominant.
e . g . i s Po s i t i ve D o m i n a n t ( [ 1 , 1 , - 2 , - 3 ] ) - > f a l s e
i s Po s i t i ve D o m i n a n t ( [ 1 , 2 , 3 , - 1 , - 2 ] ) - > t r u e
Solution:
21 positive dominant
21 positive dominant
P ro b l e m :
A n a r ray i s p o s i t i ve d o m i n a n t i f i t c o n t a i n s s t r i c t l y m o re
u n i q u e p o s i t i ve va l u e s t h a n u n i q u e n e g a t i ve va l u e s . Wr i t e
a f u n c t i o n t h a t re t u r n s t r u e i f a n a r ray i s p o s i t i ve
dominant.
e . g . i s Po s i t i ve D o m i n a n t ( [ 1 , 1 , - 2 , - 3 ] ) - > f a l s e
i s Po s i t i ve D o m i n a n t ( [ 1 , 2 , 3 , - 1 , - 2 ] ) - > t r u e
Solution:
w e c re a t e t w o S e t s , o n e w i t h a n a r ray o f p o s i t i ve va l u e s
f ro m a r r, a n d t h e o t h e r w i t h n e g a t i ve s va l u e s f ro m a r r
b e c a u s e a va l u e i n t h e S e t m ay o n l y o c c u r o n c e , w e
g u a ra n t e e t h a t a l l t h e va l u e s a re u n i q u e
. s i ze a t t r i b u t e o f a s e t re t u r n s t h e s i ze o f t h e s e t , h o w
m a n y va l u e s d o e s i t h o l d . I s p o s i t i ve d o m i n a n t i f t h e
s i ze o f t h e p o s i t i ve s e t i s l a rg e r t h a n t h e s i ze o f t h e
n e g a t i ve s e t .
21 positive dominant
22 Two Distinct Elements
Problem:
In each input array, every number repeats at least once,
except for two. Write a function that returns the two
unique numbers.
e.g. returnUnique([1, 9, 8, 8, 7, 6, 1, 6]) ➞ [9, 7]
Solution:
We filter the array using the following condition; the
first index position of the element in the array has to be
the same as the last index position of the element. This
guarantees that the element is unique, because it
doesn’t occur again in the array.
22 Two Distinct Elements
23 Reverse the Odd Length
Words
Problem:
Given a string, reverse all the words which have odd
length. The even length words are not changed.
e.g. reverseOdd("Bananas") ➞ "sananaB"
Solution:
we convert the string into an array of words by using
str.split(“ “)
we check if the word has odd characters by checking
the remainder of w.length/2. If the remainder is 0 it
means the word has even characters, otherwise it has
odd characters
if it has odd characters, we split the words characters
into a new array and we reverse it to return the
reversed word
last, we join the words with a space between them
23 Reverse the Odd Length Words
24 Hours Passed
Problem:
Write a function that takes time t1 and time t2 and
returns the numbers of hours passed between the two
times.
e.g. hoursPassed("3:00 AM", "9:00 AM") ➞ "6 hours"
Solution:
we replace AM or PM with an empty string or with +12
respectively.
we also replace :00 with an empty string as we only
care about the hours
if after doing so, t1 == t2 we set t1 to 0 and return the
difference between t1 and t2. We return no time has
passed if t1 == t2.
24 hours passed
25 White spaces
Problem:
Write a function that inserts a white space between
every instance of a lower character followed immediately
by an upper character.
e.g. whiteSpaces(“HelloMyNameIsGeorge”) -> Hello My Name Is George
Solution:
we use the replace function against a regular
expression to solve this
/([a-z][A-Z])/g will match all the lower case letters
followed immediately by an uppercase letter. e.g. oM,
yN
the second parameter of the replace function can take
a callback function with the maches from the regular
expression, in our case lower is the lowercase (o,y,e,s)
and the upper is the uppercase letter (M,N,Y,G
we replace those by adding a space between them.
25 white spaces
26 PRime numbers
Problem:
Create a function that returns true if there's at least one
prime number in the given range (n1 to n2 (inclusive)),
false otherwise.
e.g. primeInRange(10, 15) ➞ true // Prime numbers in range: 11, 13
26 prime numbers
26 PRime numbers
Problem:
Create a function that returns true if there's at least one
prime number in the given range (n1 to n2 (inclusive)),
false otherwise.
e.g. primeInRange(10, 15) ➞ true // Prime numbers in range: 11, 13
Solution:
we iterate from n1 (low) to n2 (high), and we call an
aux function isPrime() on each number. It there is one
number that is prime we return true
to check if a number is prime, we check the remainder
of dividing the number by num. If the remainder is 0
then it means the number is not prime, otherwise it’s
prime.
26 prime numbers
27 Map the Letters in a
String
Problem:
Given a word, create an object that stores the indexes of
each letter in an array
Make sure the letters are the keys
Make sure the letters are symbols
Make sure the indexes are stored in an array and
those arrays are values.
e.g. mapLetters("dodo") ➞ { d: [0, 2], o: [1, 3] }
Solution:
we create an array from the string and we apply the
reduce function with an object as the accumulator
on each iteration we return whatever was in the obj
before (...obj) and then in the char key ([char]) we
spread the previous array if it exists, otherwise we just
add the index to the array like [...(obj[char] || []), index]
27 Map the Letters in a String
28 Multiplication Table
Problem:
Create N x N multiplication table, of size n provided in
parameter.
For example, when n is 5, the multiplication table is
1, 2, 3, 4,
2, 4, 6, 8, 1
3, 6, 9, 12, 1
4, 8, 12, 16, 2
5, 10, 15, 20, 25
28 multiplication table
28 Multiplication Table
Problem:
Create N x N multiplication table, of size n provided in
parameter.
For example, when n is 5, the multiplication table is
1, 2, 3, 4,
2, 4, 6, 8, 1
3, 6, 9, 12, 1
4, 8, 12, 16, 2
5, 10, 15, 20, 25
Solution:
we use Array.from function that takes a callback fn as
second parameter to initialize each element
we also use Array.from to create another array for the
nested arrays
On each iteration we use the index of the first array (i)
as base number for multiplication
On each iteration we use the index of the second array
(j) as the multiplier which will always range between (0-
n)
28 multiplication table
29 Finding Common
Elements
Problem:
Create a function that takes two "sorted" arrays of
numbers and returns an array of numbers which are
common to both the input arrays.
commonElements([-1, 3, 4, 6, 7, 9], [1, 3]) ➞ [3]
commonElements([1, 3, 4, 6, 7, 9], [1, 2, 3, 4, 7, 10]) ➞ [1, 3, 4, 7]
Solution:
we filter the second array by the following condition: if
the arr1 has the current element in the iteration, then
we return that element, because it means it’s in both
arrays.
29 finding common elements
30 Nth Fibonacci
Number
Problem:
Create a function that returns the Nth number in the
Fibonacci sequence
fibonacci(10) ➞ "55"
fibonacci(20) ➞ "6765"
Solution:
the base case for fibonacci is when the number is less
than 2, in that case we return the number itself
for the remaining cases we return the result of running
fib(n - 1) + fib(n - 2) because for any given number the
result of a fibonacci sequence is the sum of the last
two.
30 Nth Fibonacci Number
Thank you for taking the time to go through my
Javascript interview questions and challenges.
I hope that my content has provided you with valuable
insights and knowledge that can help you improve
your skills as a Javascript developer.
If you found it helpful and want to take your skills to
the next level, I highly recommend checking out my
content on HTML and CSS, UX/UI and more here:
https://georgemoller.gumroad.com/.
Thank you once again for your interest in my
Javascript interview questions and challenges, and I
wish you all the best in your learning journey.