This is the third and sadly the last part of this series. In this part we are going to see some extra but interesting algorithmic questions when preparing for a Javascript Interview.

Let's start.

Questions 🤔

**1. Write a function that checks if a string is an anagram of another one.**

A string can have many permutations, but essentially all characters of that string will have the same frequency table. So to check if one string is an anagram of another string we just need to compare their frequency table if they are the same.

We have seen before how to compute a frequency table so we will just make it work for two strings.

Here is the gist of the code:

**2. Write a function that curries the arguments of a function. So for example instead of taking all the arguments at the same time, it will return a new function that takes the first one and return, then take the second one and return and so on.**

You will need to invoke it for example as:

```
function mul(a, b, c) {
return a * b * c;
}
let curriedMul = curry(mul);
curriedMul(1)(2)(3) // 6
curriedMul(1)(5, 3) // 15
curriedMul(1, 3, 3) // 9
```

We need a function `curry`

that accepts another function as a parameter. Then we need a way to check if we passed fewer arguments in each call and if so we can call the `curry`

function again with those arguments. Otherwise when we have all the arguments so we call the original function with those arguments.

Here is the gist of the code:

**3. Write a function that given two sorted lists you return a list with all their elements merged and sorted.**

This is the familiar merge part of the merge sort algorithm. You should be very familiar with it and able to explain how it works.

The idea is to iterate over the two arrays keeping an index for each one of the iterations i and j. We compare arr1[i] with arr2[j] and put the smallest element in the result table. Then we increase the index of the array with the smallest element.

At the end we need to make sure to move the rest of the elements if we've finished iterating one array but still have more in the other.

Here is the gist of the code:

**4.Write a function that accepts two dates and returns the number of days that have in difference.**

When you create two Date objects and subtract them, then the result is the number of milliseconds between them. For example:

```
let d = new Date()
// Mon Feb 03 2020 07:23:09 GMT+0000 (Greenwich Mean Time)
let m = new Date()
// Mon Feb 03 2020 07:23:18 GMT+0000 (Greenwich Mean Time)
console.log(d - m) // -8406
console.log(m - d) // 8406
```

So if we can convert milliseconds in days we can return the difference in days.

Here is the gist of the code:

**5. Write a function that accepts a string and removes any characters that present more that once in that string.**

We can keep count of the existing occurrences of a string char using either a set or a map. So we just iterate over the characters and if we haven't seen the last one we push it into a list. Then we use `join`

to return the result.

Here is the gist of the code:

**6. Write a function that accepts an object and returns a deep frozen copy of it's internal objects and functions.**

Here we need to do the following:

- Freeze the object using
`Object.freeze`

. - Recursively call the same freeze function for any function or object properties of that object.

Here is the gist of the code:

**7. Write a function that given a list and an index position in that list, will return a list with all elements less that the element at the index left of it and all the elements greater that the index right of it.**

This is the partition method of the Quicksort algorithm.

You should be very familiar with it and able to explain how it works.

The idea is to use the index position as the pivot point. Then have two iterators, one starting from the beginning and then other starting from the end. First use the left iterator to find an element less than pivot. Then use right iterator to find an element greater than pivot.

If both are found then swap their places in the array. The loop breaks when both iterators have crossed with each other.

Here is the gist of the code:

**8. Write a function that converts a decimal number into binary.**

The idea behind this is that every time we take the modulo of the number that represents the last bit set. For example:

```
2 --> 10 == 2 % 2
3 --> 11 == 3 % 2
5 --> 101 == 5 % 2
```

So we can calculate the last bit. Then to calculate the last but one bit we need to take the floor of num / 2 and so on. Read this wiki for more details.

Here is the gist of the code:

**9. Write a function that converts a binary number into decimal.**

Here we have a binary string and we want to convert into integer. First we traverse the string from the end. Each time we find a `1`

we use that index to get the value of 2 exp i. Then we add it to the result. For example the number 34 in binary is 100010 so we have:

```
1 * 2 ^ 8 + 1 * 2 ^ 1 = 32 + 2 = 34
```

Here is the gist of the code:

**10. Write a function that given a string it returns the list of its permutations.**

For example:

```
in: "ab" out: ["ab", "ba"]
```

This is the most tricky question. The idea is to use recursion to construct one permutation from the string of characters. Then we backtrack to produce the next permutation and so on.

For a simple example of two chars: "ab"

First we fix "a" in place and we call permutate for the remainder string "b". Because "b" is the last character we have the sequence "ab" so we add it to the result.

Then we fix "b" in the front and we call permutate for the remainder string "a". Because "a" is the last character we have the sequence "ba" so we add it to the result.

Similarly we can do that for any string of length n.

Here is the gist of the code:

## It's not the end yet

In this article we listed some warmup exercises with detailed answers. We wanted you to have a good variety of questions as a reference before your next big interview. Stay put for more articles regarding programming interviews in the future.

# Interested in Mentoring or Training?

Contact me via www.techway.io for more information.

## Discussion

REMOVE DUPLICATE... Lazy way

ANAGRAM CHECK... Lazy way

PS: I learned a lot. Thank you.

This is one of the best collection of codes to refresh mind. Thanks.

I want to say some point which I think should be applied to increase performance.

1st one frequency check

Check for length of string first

5th removing duplicate

You are checking if element is there in set. Instead do set.add(c) based on tru false push it to constant

6th deep freeze.

I don't know what is deep freeze. But when I clicked on run. I got erro "RangeError: Maximum call stack size exceeded

at Function.hasOwnProperty in core — line 0

"