Interview Q&A / JavaScript

INTERVIEW Q&A · 71 QUESTIONS

JavaScript interview questions

Closures, the event loop, this, promises and the core language questions every frontend round asks.

  1. What is the difference between debounce and throttle? Implement both.

    Debounce waits until calls stop for a set time and then runs the function once, like search-as-you-type. Throttle runs the function at most once per time window no matter how often it is called, like handling scroll events. Both are built with a closure that holds a timer or a timestamp.

    IntermediateIn-depthCoding3 min read
  2. How do you remove duplicate values from an array?

    The shortest way is [...new Set(arr)], because a Set stores each value once and keeps insertion order. Filtering with indexOf also works but is slower. For objects, dedupe by a key using a Map.

    BeginnerQuick answerCoding1 min read
  3. Write a function once(fn) that lets fn run only the first time it is called.

    Return a wrapper that keeps a called flag and the first result in a closure. On the first call, run fn and store the result; on later calls, return the stored result without running fn.

    BeginnerQuick answerCoding1 min read
  4. Write a function that returns the first non-repeating character in a string.

    Count how many times each character appears using a Map in one pass, then scan the string a second time and return the first character whose count is 1. Return null if none exists.

    BeginnerQuick answerCoding1 min read
  5. Write a function to reverse a string and use it to check for a palindrome.

    Split the string into characters, reverse the array and join it back. A palindrome reads the same both ways, so clean the text (lowercase, letters and digits only) and compare it with its reverse.

    BeginnerQuick answerCoding1 min read
  6. Write a function that deep clones an object, including nested arrays and circular references.

    Return primitives as they are. For objects and arrays, create an empty copy, record it in a WeakMap so circular references reuse it, then recursively clone each property. In modern runtimes, structuredClone does this for you.

    IntermediateQuick answerCoding1 min read
  7. Write a function that flattens a nested array of any depth.

    Loop through the array; if an item is an array, flatten it recursively and add its items, otherwise add the item. The built-in shortcut is arr.flat(Infinity).

    IntermediateQuick answerCoding1 min read
  8. Write a memoize function that caches the results of a pure function.

    Wrap the function and store results in a Map keyed by its arguments. If the key exists, return the cached value; otherwise compute, store and return. It only works safely for pure functions.

    IntermediateQuick answerCoding1 min read
  9. Write a retry function with exponential backoff for an async call.

    Loop up to a maximum number of attempts, awaiting the call in a try/catch. On failure, wait delay times 2 to the power of the attempt, then try again. After the last failure, throw the last error.

    IntermediateQuick answerCoding1 min read
  10. Write your own version of Function.prototype.bind.

    Add a method on Function.prototype that remembers the original function, the chosen this and any preset arguments, and returns a new function that calls the original with apply, joining preset and new arguments.

    IntermediateQuick answerCoding1 min read
  11. Write your own version of Promise.all.

    Return a new promise. Keep a results array and a counter. Wrap each input in Promise.resolve, store its value at its index, and resolve when the counter reaches zero. Reject as soon as any input rejects.

    IntermediateQuick answerCoding1 min read