Top 30 Javascript Interview Warmup Exercises
Theofanis Despoudis
Posted on January 6, 2020
Read the original in CodeThat.today
A lot of times when we have upcoming interviews there are cases when you will be asked to do a technical task in front of the reviewer in your language of choice. Because this phase is the most critical for your success, it's important to be prepared and at least be more confident with your programming skills.
So with this article we are going to list the most important warmup exercises for Javascript interviews. The type of exercises are simple, basic questions that ask you to write a simple function and expand it further if necessary.
This is not meant to be a complete interview prep because the interviewer could ask more advanced questions. They are however good enough for stretching your memory.
Here we go then. The top 30 Javascript warmup exercises for interview preparation. We list the first 10 out of 30 questions in this Part.
Questions π€
Here is the complete list algorithms together with detailed explanations:
- 1. Write a function the reverses a string.
Javascript does not have a build in String Builder class so you cannot modify an existing string. What we can do is create a list that we push
each character from the original string starting from the end.
Then we use Array Join to combine the characters as the reversed string.
Here is the gist of the code:
- 2. Write a function that filters out numbers from a list.
We can filter the list and remove anything that is not a number. How do we check if a list item is not a number? Well if we use the typeOf operator we can get:
typeof 1 // number
but if the interviewer asks that valid numbers are strings are also allowed we get:
typeof "1" // string
which is not what we need. The solution is to use isNaN function.
However if you noticed (and maybe the interviewer is picky) there are two cases where this thing fails:
isNaN('') //false
isNaN(true) //false
isNaN(null) // false
So we want to add three more checks for empty string, boolean and null check:
function isBoolean(value) {
return typeof value === 'boolean';
}
function isEmptyString(value) {
return typeof value === 'string' && value.trim().length === 0;
}
Here is the gist of the code:
- 3. Write a function that finds an element inside an unsorted list.
This is a typical linear search algorithm that takes Ξ(n) time to complete. We need to traverse the whole list and compare the search item with the current item:
- 4. Write a function that showcases the usage of closures.
Please review existing dev.to articles about what is a Closure. They are better at explaining the details.
Here is a simple example:
You should be able to explain where is the closure there.
- 5. What is a Promise? Write a function that returns a Promise.
Please review existing dev.to articles regarding what is a Promise. They are better at explaining the details.
Here is a simple example of a Promise:
- 6. Write a function that flattens a list of items.
This is a typical interview question. A list of lists can be flattened so that it contains only one level of items. For example: [1, [2,3, [4]]]
should flatten into [1, 2, 3, 4]
.
In order to flatten we need to recurse as we may have a deep hierarchy of lists. First we create the result list. Then we iterate over all the items and check if the item is a list. If it's not a list we append to the result. Otherwise we call the calling function again but with the contents of the item instead.
Here is the gist of the code:
- 7. Write a function that finds an element inside a sorted list.
The question seeks to test how well can you implement binary search here. So with binary search you find the middle element and then you check if it's the target element. If it's less than the target, then we know that it is located in the first half of the input array. If it's greater, then it's located in the second right half of the input array.
Here is the complete code:
- 8. Write a function that accepts two numbers
a
andb
and returns both the division ofa
andb
and their modulo ofa
andb
.
This is straightforward. Here we need to return two values:
a / b
and a % b
.
- 9. Write a function that computes the fibonacci number of N.
In the Fibonacci sequence, every element is the sum of the previous two terms. For example, starting from 0 and 1:
0, 1, 1, 2, 3, 5, 8, ...
We can do this using either recursion or just a while loop. With recursion we may fall into the trap and do it like this:
function fib(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
return fib(n-1) + fib(n-2);
}
}
And allow the interviewer to ask why is it so inefficient. Or we can just add a memoization and make it slightly better:
- 10. Write a function that accepts a string and returns a map with the strings character frequency.
To calculate the frequency we need to use a hash-table. Typically we use either an object mapping the keys to values or even more semantically a javascript Map.
We iterate over all the characters of the string and increase their char counter.
Here is the code for it:
What's next
Stay put for the next part!
πππ
Interested in Mentoring or Training?
Contact me via www.techway.io for more information.
Posted on January 6, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.