JavaScript Interview Questions P1

junior az
8 min readJan 23, 2021

Welcome back again to another important article that describes my personal experience with a couple of job interviews I have had recently. As a learning developer, you probably wanted to get into the professional field where you can get paid for your skills.

As we know most tech companies will require you to pass a whiteboarding code assessment in order to pass to the next step in your interview. Some companies like to give the candidates a take-home code assignment and other companies like to do a live coding challenge so they can see how you think and also see if they can picture themselves working with you on projects in the future if they hire you.

My advice to you is to take as many coding challenges as you possibly can, it will help you get rid of the anxiety and you will get used to that atmosphere. Getting used to those kinds of challenges will help build your confidence and help you get used to solving problems under the pressure.

It completely okay if you get stuck or don’t know the answer to some problems. In addition, interviewers do not expect you to know everything and they just want to see if you think as a problem solver, and most of the time they tell you that you can use google as a resource if you get stuck.

In this article, I would share a couple of the coding challenges I faced recently and these interviews were face-to-face problem-solving challenges. Stay tuned and sit back maybe one day you will face the same questions I did and if not at least you learn how to think through the problems.

#1-First Interview Challenge

In this question I was giving an array of names, the one question is to have a new array that contains only names that start with a certain letter. This is the array I was given that holds different names.

The question is: given an array of names print out another array that contains names that start with the letter B?

let names=["Mark", "Milan", "Bob", "John", "Bobby", "Melissa", "Brianna", "Meryam", "Billy"]

What is an Array?

The object Array allows you to store multiple values in a single variable. It stores the sequential array of elements of the same form in a fixed-size. An array is used to store a collection of information, but treating an array as a collection of variables of the same kind is often more helpful.

The Approach?

It would be a really good idea to pseudocode your thoughts on how you suppose to approach this question. Basically what we need to do first is create a function that will contain the solution.

let names=["Mark", "Milan", "Bob", "John", "Bobby", "Melissa", "Brianna", "Meryam", "Billy"]
function wordsWithB(names) {
}

I named this function wordsWithB and of course, you can choose any name that you personally prefer. As you see can this function so far has one parameter which names.

The inside of the function will hold the logic that I will use to get to the solution. In plain English, I will have to iterate through each element of my array and basically pick the names that only start with the letter B.

Keep in mind that the first index in the array starts at the number zero. So in our case, the order will be like this:

Mark => will be index number 0.

Milan => will be index number 1.

…….

Billy => will be index number 8.

Note:

I would like to mention that we have 9 elements in our so the total elements in the array is different from the index order of the array.

After understanding the array order let us move to the next step of our logic.

Next Step:

What we have to think about next is, we need to create an empty array where we can push the new elements that will start with the letters required. This step is really simple, inside of our function we will have to create a variable that has a value of an empty array and its name could be anything you would like.

let names=["Mark", "Milan", "Bob", "John", "Bobby", "Melissa", "Brianna", "Meryam", "Billy"]function wordsWithB(names) {  let bWords =[]}

I named my array bWords and as I mentioned I will use that array later and have it holding the names I am looking for which in this case they have to start with the letter B.

Next Step:

this step is really important which will determine the iteration through the length of the original array. I will be using for loop in this.

What is for loop:

The declaration produces a loop that is performed as long as a condition is valid. As long as the situation is real, the loop will continue to run. Only when the situation becomes incorrect will it end. JavaScript supports various types of loops: … For/in — loops through an object’s properties.

let names=["Mark", "Milan", "Bob", "John", "Bobby", "Melissa", "Brianna", "Meryam", "Billy"]function wordsWithB(names) {   let bWords =[]   for(let i = 0; i < names.length; i++){
}
}

Let us explain the line of code we just added right there:

We started with our for-loop as seen above after that we have i = 0 which refers to the first position of our element in the array of names, So basically we are telling the loop to start from the beginning of the array.

Then we have i < names.length which basically means that we want the iteration to stop at the end of the array, in plain English, we are asking our loop to iterate from the beginning of the array (index at position 0) all the way to the end of the array(index at position 8).

After comes the popular i++ which refers to the value of i before the increment. The value of ++i is the value of i after the increment. Example:

var i = 0,// i++ in this case will 1 every time it iterates, meaning it will keeps going up by one till the loop stops.

Next Step:

Now it is time to apply our logic after we loop through our array, we basically want to tell the for loop to iterate through that array and pick only the name that starts with the letter B.

What we need to do now is use if statement, what is if statement?

Usage and Description. When a stated condition is valid, the if/else statement executes a block of code. Another block of code may be executed if the condition is incorrect. The if/else statement is a part of the “Conditional” statements of JavaScript which are used to perform various actions based on different conditions.

let names=["Mark", "Milan", "Bob", "John", "Bobby", "Melissa", "Brianna", "Meryam", "Billy"]function wordsWithB(names) {  let bWords =[]  for(let i = 0; i < names.length; i++){
if(names[i].startsWith('B')){
} }}

Let us walk through our new line of code, basically, we wanted to use an if statement to execute a line of code if the statement meets the right condition. As you can see we used names[i] and that is targeting the entire array and then we used a built-in JavaScript method with is .startsWith(‘ ’) and inside this build-in method, you can put the letter you want to target.

Note:

The startsWith() method determines whether a string begins with the characters of a specified string.

This method returns true if the string begins with the characters, and false if not.

The startsWith() method is case sensitive.

There also endsWith()method that you can use in case you came across a challenge that wants you to sort an array that ends with a certain letter.

Last Steps:

Remember that empty array [] we created at the beginning, it is finally time to use it. What is going to happen now is our for loop is going to iterate through the array of names and based on the if statement we wrote it will find only the names that start with the letter B and after that those names will be pushed in a new array.

After that, all we need to do is return our array that was empty at the beginning but now it will contain our names that only start with the letter B, and don forget to call your function at the end.

let names=["Mark", "Milan", "Bob", "John", "Bobby", "Melissa", "Brianna", "Meryam", "Billy"]function wordsWithB(names) {  let bWords =[]  for(let i = 0; i < names.length; i++){    if(names[i].startsWith('B')){    bWords.push(names[i])   } } return bWords}wordsWithB(names)

What is push() method?

At the end of an array, the push() method adds new items and returns the new length. Note: At the end of the array, the new item(s) will be added. Note: The length of the array is altered by this process. Tip: Use the unshift() method to add objects at the beginning of an array.

So let us take a look at the final result on a code editor to see if this actually works how we wanted to.

I hope this was helpful to get you a little bit prepared for your next interview and good luck. I will also list some JavaScript basics to help you learn more about this coding language.

Type of Algorithms you should know:

1- Search Algorithm => Designed to retrieve information stored within a data structure.

Examples :

  • Linear Search.
  • Binary Search.
  • Depth-First Search (DFS).
  • Breadth-First Search (BFS)

2 - Sort Algorithm => This is used for rearranging arrays or a given list of elements based on the role of the comparison operator. The comparison operator is used to determine whether an element is a new order.

Examples :

  • Quick Sort.
  • Insertion Sort.
  • Selection Sort.
  • Bubble Sort.
  • Merge Sort.
  • Radix Sort.
  • Heap Sort.

3 - Dynamic programming => A recursion can be optimized. The dynamic program stores the results of a re-computation sub-program for future use. From exponential to polynomial time, this simple optimization reduces time complexities.

Examples :

  • Fibonacci Number Series.
  • Knapsack Problems.
  • Tower of Hanoi.
  • Shortest Path by Dijkstra.
  • Matrix Chain Multiplication.

4 — Recursive Algorithm => Which is an algorithm which calls itself with smaller or simpler input values. The result is returned back up the chain of calls until it hits the original problem.

Examples :

  • Factorial.
  • Exponential.
  • Tower of Hanoi.
  • Tree Traversals.
  • Depth-First Search.

5 — Greedy Algorithm =>Often selecting the next piece that provides the most apparent and immediate advantage, which is an algorithmic paradigm that builds up a solution piece by piece.

Examples :

  • Huffman Coding.
  • Fractional Knapsack Problem.
  • Activity Selection.
  • Job Sequencing Problem.

I know it looks like a lot to know at once and could be overwhelming but just believe in the process and you will do just fine.

--

--