DEV Community

loading...

Interview panic - how to overcome the difficult questions

vazsonyidl
I am a Software Dev Engineer at One Identity ☀️
・4 min read

Intro

There are several interviews in every developers life. You may look for a new opportunity, or you are just preparing yourself to keep this skill up-to-date and do not let this rust. The case does not matter, it could happen to you when you encounter a question where you do not have so much clue at a first glance how to solve the task. In this article, I will suggest some ideas how to overcome these though times.
This post is for beginners or pre-intermediate level but I recommend this article for everyone.

The scene

So let's take that you are on an interview and the interviewer pops up a coding question - and you are like

excuse me, what the?! I can not solve this, sorry...

Of course you should not say this - or at least not the best idea. Because of reasons and the most frequent interviewing process, you have to prove that you are the best candidate ever and you have to find the best solution. Instead of panicking, I suggest you to try the following steps because I am hundred percent sure that the interviewer's goal is not to see the most efficient solution within 2 minutes in a one-liner.

Ideas

1) Taking your possibilities into account

If you have a code editor open - and you must have one which you are familiar with, start to sketch your possible options and functions you already now. Let's write down the data structures which could come in handy. You may think this is not the best idea, but while you are noting that - I have Set, Map, Array and Object for this, hmm Objects are not the best for this, then two remains. Now, if you are thinking loud, the interviewer can follow your thoughts, maybe ask a question - what is the main difference of use-cases between Objects and Maps? .....

2) Asking for details

This may sound stupid, but with this you can widen your knowledge of the current problem, and with that you have a more clear vision of what to implement. From another perspective, it is always a good point during the interview process. Working in a team and implementing feature is full of discussion and questions, so know how to ask and what to ask is a really valuable skill.

3) Put on the TDD glasses

This abbreviation stands for Test Driven Development, and nowadays this is a popular concept in the software development industry. This could be a whole Udemy course what TDD is and what are the best practices to start with, etc. but you can start with some tests, where you can summarise your current informations and requirements for the task. This can give a skeleton for your possible solution. Write a test skeleton for each requirements you have discovered within the last few minutes, then go ahead and start implementing your solution.

With these, you have more and more time to think about the solution for the original question, instead of just sitting there silently which could be a quiet awkward situation. Do not forget that the interviewer's goal is not to find the most optimised solution within 45 seconds but to see your level of skills - soft skills and coding skills as well.

This may have sounded like that you need half an hour for solving a simple task, but believe me that you will go through this points within 3 to 5 minutes. One thing to add of course - you do not have to use these all the times, or at all.

Example

Implement a stack without usage of an array!

Now let's quickly summarise the things and go through our bullet points.

1) The remaining options in JavaScript are /excluding primitive types, but I really welcome a primitive type stack solution in the comment section :)/ :

  • Map
  • Set
  • WeakMap
  • WeakSet
  • Object

Knowing the behaviour of a stack, we may exclude Map and WeakMap, hence we should now what key we are querying for.

Set and WeakSet are dropped because they store unique reference values, so if you would like to add the same primitive value twice in your solution, you will not be able to.

So, Object remained.

2) Ask for questions:

  • Does a maximum/default stack size required?

Yes, maybe let's take this to 7

  • What should happen when we overflow/underflow our stack?

Throw some kind of error accordingly.

3) Sketch up some TDD things

With all the information you have by this time, you can sketch up your tests' skeleton.

describe('Stack without array', () => {
  it('should be initialised - with default length', () => {
    let stack = new Stack();
    expect(stack).toBeDefined();
});

  it.todo('should set maximum length');

  it.todo('should add new item on top');

  it.todo('should remove item from top');

  it.todo('should handle overflow');

  it.todo('should handle underflow');
});
Enter fullscreen mode Exit fullscreen mode

Solution:

class Stack {
  #elements = {};
  constructor(private size: number = 7) {
  }

  get elements() {
    return this.#elements;
  }

  public push(element) {
    this.#elements[Object.keys(this.#elements).length] = element;
  }

  public pop() {
    this.#elements = Object.entries(this.elements)
      .filter(([index, element]) => +index !== Object.keys(this.elements).length - 1)
      .reduce((acc, [index, element]) => {
        acc[index] = element;
        return acc;
      }, {});
  }
}
Enter fullscreen mode Exit fullscreen mode

Without implementing every detail, you may see how can we iterate over implementing this and extending the test cases in paralel while finding the solution.

Outro

Hope you enjoyed my article, please add any interview idea here as well as correcting me for any mistakes I have made. Have a nice day everyone!

Discussion (0)