Js Interview Challenges
Js Interview Challenges
Js Interview Challenges
Interview Questions
& Challenges
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 ] )
Problem:
Solution:
It is possible with simple destructuring assignment using
array
02 VARIABLES SWAP
03 stairs problem
Problem:
## ##
###
Solution:
03 STAIRS PROBLEM
04 Array check
Problem:
Solution:
You can use Array.isArray() method
04 ARRAY CHECK
05 age difference
Problem:
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:
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
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:
Solution:
Lowercase the string and use the RegExp to remove
unwanted characters from it
07 palindrome
08 vowels
Problem:
Solution:
The match() method retrieves the result of matching a
string against a regular expression
08 vowels
09 NULL VS UNDEFINED
Problem:
Solution:
Undefined: means a variable has been declared but has
not yet been assigned a value
09 null vs undefined
10 Reverse a string
Problem:
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:
var
let
const
Problem:
length 2
13 chunk problem
13 Chunk problem
Problem:
length 2
Solution:
create an auxiliary array
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:
Solution:
create an auxiliary words array and iterate through
each word in the string
14 capitalize words
15 reverse integers
Problem:
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
15 reverse integers
16 First non repeating
character.
Problem:
Problem:
Solution:
create a char auxiliary variable to store each charater
on the sentence.
Problem:
17 highest occurrence
17 highest occurrence
Problem:
Solution:
if array has no items we return nul
17 highest occurrence
18 intersection
Problem:
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:
Solution:
Object.keys returns an array with all the keys of the
object
19 make pairs
20 Total Price
Problem:
Solution:
20 total price
20 Total Price
Problem:
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:
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.
Problem:
Solution:
we convert the string into an array of words by using
str.split(“ “)
Problem:
Solution:
we replace AM or PM with an empty string or with +12
respectively.
24 hours passed
25 White spaces
Problem:
Solution:
we use the replace function against a regular
expression to solve this
25 white spaces
26 PRime numbers
Problem:
26 prime numbers
26 PRime numbers
Problem:
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
26 prime numbers
27 Map the Letters in a
String
Problem:
Solution:
we create an array from the string and we apply the
reduce function with an object as the accumulator
Problem:
28 multiplication table
28 Multiplication Table
Problem:
Solution:
we use Array.from function that takes a callback fn as
second parameter to initialize each element
28 multiplication table
29 Finding Common
Elements
Problem:
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.
Number
Problem:
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.
https://georgemoller.gumroad.com/.