Getting a job in big four companies is a dream for every graduate. It is the best place to start your career with. Today in this article we will discuss some important questions asked by big four companies and how to solve them. Please follow optjobs blog for many more such articles. So Let’s begin-

As a first step, the big four companies have a first round as a coding round in which they testify your technical problem solving skills. Here we are focusing on front end developer coding interview round.

The main reason to have coding round it not to check your tech depth, it is to test you problem solving approach. So how to approach the problem.

1. Create a strong foundation -

If you are lacking a formal Computer Science background, or simply find that the recommended problem solving techniques for coding interviews aren’t really helping you, then your first step should be to dedicate yourself to learning all of the fundamental data structures and algorithms.

2. More coding experience -

The key here is simply to get more practice coding, and ideally do so in an environment where you are getting good feedback on your code. One of the best ways to do this is by contributing to open source projects.

3. Strategically approach

The most common plan I see to solving interview problems looks something like the following:

  1. Look at the problem
  2. Think about the problem
  3. Come up with a solution
  4. Write the solution
  5. Success

4. Start with the brute force solution

I’ll tell you that finding a brute force solution is 1000% better than not finding a solution at all. And if you start by immediately trying to find the optimal solution, it is easy to get stuck and end up without a complete solution by the end of the interview.

So now Let’s Solve a problem asked in Microsoft Interview Recently -

Implement Promise.all

Promise.all

The Promise.all() method returns a single Promise that resolves when all of the promises passed as an iterable have resolved or when the iterable contains no promises. It rejects with the reason of the first promise that rejects.

Question

Write a function newPromiseAll which takes an array of Promises as an argument, and returns a promise, which if resolved, returns the result of each promise in an array, or an error catch block, if one of the promises is rejected.

newPromiseAll(promises)
.then(results => {
})
.catch(e => {
})

Thought process Pseudocode

  1. For the sake of simplicity we will create a method task which will create a promise and resolve it in some time x.
function task(time) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve(time);
    }, time);
  });
}
  1. Since the order of the results array is important, we will chain each promise resolution to the next one
  2. A simple forEach iterator can be used, and we need to keep track of the index of the parent array
  3. When the number of promises resolved is equal to the length of the task array we will resolve the overall results array.

Give it a try!!

Solution

function task(time) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve(time);
    }, time);
  });
}
const taskList = [task(1000), task(5000), task(3000)];
// returns promise with results in an array
function myPromiseAll(taskList) {
  const results = []
  let promisesCompleted = 0;
  return new Promise((resolve, reject) => {
    taskList.forEach((promise, index) => {
      promise.then((val) => {
        results[index] = val;
        promisesCompleted += 1;
        if (promisesCompleted === taskList.length) {
          resolve(results)
        }
      })
        .catch(error => {
          reject(error)
        })
    })
  });
}
myPromiseAll(taskList)
  .then(results => {
    console.log("got results", results)
  })
  .catch(console.error)

And we are done. So keep practicing and follow us for such cool blogs.

Optjobs is a job portal for helps OPT students to get a job.