Interviewer: Senior Developer? Let’s See How You Handle This Code Challenge

Why Experience Alone Isn’t Enough: The Art of Continuous Improvement

Photo by Christina @ wocintechchat.com on Unsplash

In Atlassian’s final round, I was thrown a curveball with this question, and let me tell you, it knocked me flat. I was completely blindsided — no idea what hit me.

So here is the story.

Interviewer: Here is a small piece of code, go through it, review it, and let us know in case you find any scope for improvement in it.

Code for revew

I stared at the code for a while and gave up because at that time it seemed ok to me.

However, after the interview was over, I asked the interviewer about this question.
So he told me two things.

Improvement 1: Remove the arrow function passed in setTimeout

In the setTimeout we are passing an arrow function, just to wrap greet function, because we have to pass FrontendMaster string an argument.

But setTimeout, accepts additional arguments that can be directly supplied to the function passed as callback.

Syntax to setTimeout

setTimeout(callbackFn, delay, arg1, arg2, ...);

• callbackFn: The function to be executed after the timer expires.
• delay: The time in milliseconds after which the function should be executed.
• arg1, arg2, ...: Optional arguments that will be passed to the function when it is executed.

After Improvement:

after improvment

Well, this was an enlightening moment for me. I had no clue that we could pass additional arguments to setTimeout. This is how things work. You always learn.

Improvement 2: Unnecessary closure

In the greet the function we are using channel variable from init function, and we are creating a closure.

The problem with closure:

Closure captures the whole scope: Even though we are only using channel but the whole scope of init function will be captured.

Closure Retains Variables: The greet function forms a closure over the init function’s scope, capturing the channel variable. As a result, even after init finishes execution, the channel variable will still be retained in memory until the greet function is executed by setTimeout.

  • Memory Impact: If init is called frequently, and each time it captures a different variable in a closure, this could lead to increased memory usage. In large applications or tight loops, this might become a memory leak if not handled carefully.

Here is the improved version of the code:

After 2nd Improvement.

Well I had a little hunch that this is something that we can avoid, but wasn’t sure.

FYI: I don’t clear that round. And this question might be the reason.

#javascript #frontendinterview

--

--