11 questions from the interview for a front-end developer

11 questions from the interview for a front-end developer

An interview for a job is a good opportunity to test your skills. So, despite the results, you gain experience. Here I want to share the answers for the questions they asked me in my latest interview for a junior front-end developer position.

Test task

Before the interview, they gave me a test task. I had to create a movie catalog with movie cards and a search. I could make any design layout I wanted, but it had to be responsive.

You can see the final result on my github.

Questions

After completing the task, they invited me to the technical online interview. Below is a list of questions and answers.

1. What’s the difference between var, let, and const?

All of these are keywords for declaring variables in JavaScript.

var

  • declares a variable with function or global scope
  • can be re-declared
  • can be accessed before declaration because of hoisting
  • the value can be changed

let

  • declares variable with block scope
  • can’t be re-declared
  • can’t be accessed before declaration (causes a ReferenceError)
  • the value can be changed

const

Same as let except:

  • the value must be assigned during declaration and can’t be changed

2. What’s the difference between cookies, localStorage and sessionStorage?

cookies

  • Sent with every HTTP request
  • Small size (about 4 KB).
  • Can be expired
  • Used for auth, sessions, server-side needs.
  • Vulnerable to CSRF
  • HttpOnly cookies can’t be read by JS. That makes them resistant to XSS attacks

localStorage

  • Not sent to the server. Stored only in the browser and shared across all tabs of the same origin.
  • Big size (5-10 MB).
  • Persists until explicitly cleared.
  • Used only for non-sensitive data
  • Vulnerable to XSS
  • Fully accessible via JS

sessionStorage

Same as localStorage except:

  • Isolated per tab.
  • Cleared when the tab/window is closed.

3. Phases of rendering content in a browser

  1. Parsing HTML – building DOM and CSSOM trees
  2. Building the Render Tree – combining DOM and CSSOM trees. Elements that won’t be displayed, such as head and elements with { display: none }, are excluded from the Render Tree
  3. Layout (Reflow) – calculating geometry: sizes and positions
  4. Paint Phase – the browser displays the actual page
  5. Compositing – this phase happens only if the browser breaks the page into layers to improve performance. Compositing orders all layers correctly.

4. How to debug JS code

  • DevTools – console, network, breakpoints, etc
  • console.log() – to show data in the console
  • debugger; to stop execution in a specific place
  • node --inspect for Node.js

5. How to center div block horizontally

  • margin
  • position
  • justify-self
  • flex
  • grid
HTML
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Central Div Horizontally</title>
  <style>
    .block {
      border: 1px solid #aaa;
      border-radius: 8px;
      padding: 16px;
      width: fit-content;
      margin-bottom: 16px;
    }

    .center-margin {
      margin: 0 auto 16px auto;
    }

    .center-position {
      position: relative;
      left: 50%;
      transform: translateX(-50%);
    }

    .center-justify-self {
      justify-self: center;
    }

    .center-flex {
      display: flex;
      justify-content: center;
    }

    .center-grid {
      display: grid;
      place-items: center;
    }
  </style>
</head>

<body>
  <div class="block center-margin">Block</div>
  <div class="block center-position">Block</div>
  <div class="block center-justify-self">Block</div>
  <div class="center-flex">
    <div class="block">Block</div>
  </div>
  <div class="center-grid">
    <div class="block">Block</div>
  </div>
</body>

</html>

6. What does the requestAnimationFrame method?

requestAnimationFrame() tells the browser that you want to animate an element with JavaScript. The browser will execute a callback function that you pass as a parameter before the next repaint. But it will do this only once. To continue the animation, you need to call requestAnimationFrame() repeatedly.

HTML
<img id="bee-dog" src="beedogpin.webp" alt="bee dog">
<script>
  const element = document.getElementById('bee-dog');
  let rotation = 0;

  function animationStep() {
    element.style.transform = `rotate(${rotation++}deg)`;
    requestAnimationFrame(animationStep);
  }

  requestAnimationFrame(animationStep);
</script>

7. CSS-animations

You can create an animation in CSS with the @keyframes rule. To run it, assign the animation-name property to an element. There are also a bunch of properties to change animation parameters: duration, direction, iteration count, etc.

CSS
@keyframes animationName {
  from {
    width: 100px;
  }
  to {
    width: 200px;
  }
}

div {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  animation-name: animationName;
  animation-duration: 4s;
  animation-delay: 2s;
  animation-iteration-count: 3;
  animation-direction: alternate-reverse;
  animation-timing-function: ease-in-out;
  animation-fill-mode: both;
}

8. What’s the difference between authentication and authorization?

  • Authentication verifies who you are.
  • Authorization determines what you are allowed to do after authentication.

9. What is event delegation?

Event delegation is a pattern where you attach a single event listener to a parent element instead of individual listeners on each child. It helps improve performance because there is only one event listener for several elements.

HTML
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
</ul>
<script>
  document.querySelector('ul').addEventListener('click', (e) => {
    const item = e.target.closest('li');

    if (!item)  { 
      return;
    }
    
    console.log(item.textContent);
  });
</script>

10. How does garbage collector work?

JavaScript’s Garbage Collector uses Mark-and-sweep algorithm. Its goal is to find unreachable objects. It does this by scanning all references starting from the root. If an object has at least one reference that is reachable from the root (including through other references), it’s considered reachable and is marked as alive. The garbage collector removes unmarked objects from memory.

JavaScript
let obj = { name: 'Object name' };

obj = null;

The variable obj has access to the object { name: 'Object name' } by reference. When the variable changes its value, the old reference is broken, and no code has access to the object { name: 'Object name' }. Thus, the object is considered unreachable and will be removed from memory by garbage collector.

A developer can’t trigger the Garbage Collector. All they can do is remove references as soon as they become redundant.

11. In which order will code run?

JavaScript
console.log('1');

setTimeout(() => console.log('2'), 0);

Promise.resolve()
  .then(() => {
    console.log('3');
    return 'next';
  })
  .then(() => {
    console.log('4');
  });

console.log('5');

new Promise((resolve) => resolve())
  .then(() => {
    console.log('6');
  });

Answer: 1 5 3 6 4 2

First, synchronous code runs (1, 5). Asynchronous callbacks are queued: setTimeout goes to the task (macrotask) queue, and promises go to the microtask queue.

The event loop processes the microtask queue first, until it is empty. So 3 runs first and queues 4. Then 6 runs, followed by 4. (3, 6, 4)

Once the microtask queue is empty, the task (macrotask) queue is processed, so 2 runs.

Live-coding session

The live-coding session was dedicated to algorithms. The task was to complete a Queue class using two stacks only.

The initial code was:

JavaScript
// You can use push, pop and get the last item from an array
class Queue {
  constructor() {
    this.stackIn = [];
    this.stackOut = [];
  }

  enqueue(value) { }

  dequeue() { }

  peek() { }

  isEmpty() { }
}

const q = new Queue();

console.log('Is queue empty?', q.isEmpty()); // true

q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

console.log('First element:', q.peek()); // 10

console.log('Dequeue:', q.dequeue()); // 10
console.log('Dequeue:', q.dequeue()); // 20

console.log('First element:', q.peek()); // 30

q.enqueue(40);

console.log('Dequeue:', q.dequeue()); // 30
console.log('Dequeue:', q.dequeue()); // 40

console.log('Is queue empty?', q.isEmpty()); // true

At first, I was baffled because I didn’t fully understand what I needed to do or why anyone would need this. Well, I’m not good in algorithms. It took some time, but I managed to do it. The interviewer accepted my code. It was messy, and I cleaned it up after the interview. I also added a Stack class to make the limitations clear.

JavaScript
class Stack {
  constructor() {
    this.array = [];
  }

  push(value) {
    this.array.push(value);
  }

  pop() {
    return this.array.pop();
  }

  getLast() {
    return this.array.at(-1);
  }
}

class Queue {
  constructor() {
    this.stackIn = new Stack();
    this.stackOut = new Stack();
  }

  enqueue(value) {
    this.stackIn.push(value);
  }

  dequeue() {
    let currentElement;
    while (currentElement = this.stackIn.pop()) {
      this.stackOut.push(currentElement);
    }

    const result = this.stackOut.pop();

    while (currentElement = this.stackOut.pop()) {
      this.stackIn.push(currentElement);
    }

    return result;
  }

  peek() {
    let currentElement;
    while (currentElement = this.stackIn.pop()) {
      this.stackOut.push(currentElement);
    }

    const result = this.stackOut.getLast();

    while (currentElement = this.stackOut.pop()) {
      this.stackIn.push(currentElement);
    }

    return result;
  }

  isEmpty() {
    return !this.stackIn.getLast() && !this.stackOut.getLast();
  }
}

const q = new Queue();

console.log('Is queue empty?', q.isEmpty()); // true

q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

console.log('First element:', q.peek()); // 10

console.log('Dequeue:', q.dequeue()); // 10
console.log('Dequeue:', q.dequeue()); // 20

console.log('First element:', q.peek()); // 30

q.enqueue(40);

console.log('Dequeue:', q.dequeue()); // 30
console.log('Dequeue:', q.dequeue()); // 40

console.log('Is queue empty?', q.isEmpty()); // true

Conclusion

After the interview, they didn’t give me a job offer, but I’m greatful to them for the feedback and the opportunity to learn something new.

As we know from survivorship bias, the experience from those who failed can sometimes be more valuable than that of those who succed. I hope this post will be usefull to somebody.

Sources