5 commonly asked frontend interview questions in product-based companies

Implement a debounce function

Debouncing is a technique to limit the rate at which a function can fire.

function debounce(func, delay) {
let timeoutId;
return function (…args) {
clearTimeout(timeoutId);
timeoutId = setTimeou…


This content originally appeared on DEV Community and was authored by Mohit Verma

Implement a debounce function

Debouncing is a technique to limit the rate at which a function can fire.

function debounce(func, delay) {
  let timeoutId;
  return function (...args) {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}

// Usage
const debouncedSearch = debounce((query) => {
  console.log('Searching for:', query);
}, 300);

Create a simple todo list component

This question tests your ability to manage state and handle user interactions.

import React, { useState } from 'react';

function TodoList() {
  const [todos, setTodos] = useState([]);
  const [input, setInput] = useState('');

  const addTodo = () => {
    if (input) {
      setTodos([...todos, input]);
      setInput('');
    }
  };

  return (
    <div>
      <input
        value={input}
        onChange={(e) => setInput(e.target.value)}
      />
      <button onClick={addTodo}>Add Todo</button>
      <ul>
        {todos.map((todo, index) => (
          <li key={index}>{todo}</li>
        ))}
      </ul>
    </div>
  );
}

Implement a basic Promise

Understanding Promises is crucial for handling asynchronous operations.

class MyPromise {
  constructor(executor) {
    this.state = 'pending';
    this.value = undefined;
    this.callbacks = [];

    const resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fulfilled';
        this.value = value;
        this.callbacks.forEach(callback => callback(value));
      }
    };

    executor(resolve);
  }

  then(onFulfilled) {
    if (this.state === 'fulfilled') {
      onFulfilled(this.value);
    } else {
      this.callbacks.push(onFulfilled);
    }
  }
}

// Usage
const promise = new MyPromise((resolve) => {
  setTimeout(() => resolve('Done'), 1000);
});

promise.then(console.log);

Implement a basic event emitter

Event emitters are useful for building pub-sub systems.

class EventEmitter {
  constructor() {
    this.events = {};
  }

  on(eventName, callback) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
  }

  emit(eventName, data) {
    const eventCallbacks = this.events[eventName];
    if (eventCallbacks) {
      eventCallbacks.forEach(callback => callback(data));
    }
  }
}

// Usage
const emitter = new EventEmitter();
emitter.on('userLoggedIn', (user) => console.log('User logged in:', user));
emitter.emit('userLoggedIn', { name: 'John' });

Flatten an array of arrays

This tests your ability to work with nested data structures.

function flattenArray(arr) {
  return arr.reduce((flat, toFlatten) => {
    return flat.concat(Array.isArray(toFlatten) ? flattenArray(toFlatten) : toFlatten);
  }, []);
}

// Usage
const nestedArray = [1, [2, 3], [4, [5, 6]]];
console.log(flattenArray(nestedArray)); // [1, 2, 3, 4, 5, 6]

Can also practice more challenging questions on PrepareFrontend platform.


This content originally appeared on DEV Community and was authored by Mohit Verma


Print Share Comment Cite Upload Translate Updates
APA

Mohit Verma | Sciencx (2025-03-09T04:47:29+00:00) 5 commonly asked frontend interview questions in product-based companies. Retrieved from https://www.scien.cx/2025/03/09/5-commonly-asked-frontend-interview-questions-in-product-based-companies/

MLA
" » 5 commonly asked frontend interview questions in product-based companies." Mohit Verma | Sciencx - Sunday March 9, 2025, https://www.scien.cx/2025/03/09/5-commonly-asked-frontend-interview-questions-in-product-based-companies/
HARVARD
Mohit Verma | Sciencx Sunday March 9, 2025 » 5 commonly asked frontend interview questions in product-based companies., viewed ,<https://www.scien.cx/2025/03/09/5-commonly-asked-frontend-interview-questions-in-product-based-companies/>
VANCOUVER
Mohit Verma | Sciencx - » 5 commonly asked frontend interview questions in product-based companies. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/09/5-commonly-asked-frontend-interview-questions-in-product-based-companies/
CHICAGO
" » 5 commonly asked frontend interview questions in product-based companies." Mohit Verma | Sciencx - Accessed . https://www.scien.cx/2025/03/09/5-commonly-asked-frontend-interview-questions-in-product-based-companies/
IEEE
" » 5 commonly asked frontend interview questions in product-based companies." Mohit Verma | Sciencx [Online]. Available: https://www.scien.cx/2025/03/09/5-commonly-asked-frontend-interview-questions-in-product-based-companies/. [Accessed: ]
rf:citation
» 5 commonly asked frontend interview questions in product-based companies | Mohit Verma | Sciencx | https://www.scien.cx/2025/03/09/5-commonly-asked-frontend-interview-questions-in-product-based-companies/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.