JavaScript Functions Questions

Implement JavaScript functions like utility methods, polyfills, or DOM APIs.
Code in browser
Questions and solutions by ex-interviewers
Comprehensive test cases

Questions List

JavaScript Coding InterviewsQuestion types to expect, important concepts to know and top practice questions to do
Guide
GUIDES
  • FlattenImplement a function that recursively flattens an array into a single level deep
    Languages
  • DebounceImplement a function that delays execution until calls have stopped for a given time
    Languages
  • Debounce IIPremiumImplement a debounce function that comes with a cancel method to cancel delayed invocations and a flush method to immediately invoke them
    Languages
  • useQueryImplement a hook that manages a promise resolution
    Languages
  • Array.prototype.filterPremiumImplement the Array.prototype.filter() method
    Languages
  • ClassnamesImplement a function that conditionally joins CSS class names together
    Languages
  • Classnames IIPremiumImplement a function that conditionally joins CSS class names together and also handles de-duplication and function values
    Languages
  • useBooleanImplement a hook that manages a boolean state, with additional convenience utility methods
    Languages
  • useDefaultPremiumImplement a hook that returns the default value when state is null or undefined
    Languages
  • Promise.allImplement the Promise.all() function that resolves to an array of results if all the input elements are resolved or rejects otherwise
    Languages
  • ThrottleImplement a function that limits how often a function can run
    Languages
  • Map AsyncPremiumImplement a function that maps an array of items with an asynchronous mapping function
    Languages
  • Event EmitterImplement a class that can subscribe to and emit events that trigger attached callback functions
    Languages
  • getElementsByStyleImplement a function to get all DOM elements that are rendered using the specified style
    Languages
  • Promise.racePremiumImplement the Promise.race() function that resolves or rejects when any of the input elements are resolved or rejected
    Languages
  • Deep CloneImplement a function that performs a deep copy of a JSON-serializable value
    Languages
  • Promise.anyImplement the Promise.any() function that resolves when any of the input elements are resolved
    Languages
  • useArrayImplement a hook that manages an array of items
    Languages
  • useCounterImplement a hook that manages a counter state, with some additional convenience utility methods
    Languages
  • useCounter IIImplement an optimized version of the useCounter hook
    Languages
  • Cancellable IntervalPremiumImplement a function that acts like setInterval but returns a function to cancel the interval
    Languages
  • useCycleImplement a hook that cycles through a sequence of values
    Languages
  • Backbone ModelPremiumImplement a class resembling Backbone.Model which allows storing of attributes/values and responding to changes in specific attribute values
    Languages
  • List FormatImplement a function that formats a list of items into a single readable string
    Languages
  • Map Async LimitImplement a function that maps an array of items with an asynchronous mapping function while not exceeding the concurrency limit
    Languages
  • useStateWithResetPremiumImplement a hook that's similar to useState but with an additional reset function that resets the state to its initial value
    Languages
  • ComposePremiumImplement a function that takes multiple functions as arguments and returns a new function that applies those functions in reverse
    Languages
  • Deep EqualImplement a function that determines if two values are equal
    Languages
  • Data SelectionPremiumImplement a function to filter rows of data matching a specified requirement
    Languages
  • Type UtilitiesImplement utilities to determine primitive variable types in JavaScript
    Languages
  • Deep OmitImplement a function that removes specified keys and their corresponding values from an object, including nested objects or arrays
    Languages
  • useMapPremiumImplement a hook that manages a JavaScript map
    Languages
  • PromisifyImplement a function that takes a function following the common error-first callback style and returns a version that returns promises
    Languages
  • Promisify IIPremiumImplement a promisify function that allows the original function to override the return value
    Languages
  • jQuery.cssPremiumImplement a jQuery-like function that sets the style of a DOM element
    Languages
  • MemoizeImplement a function that returns a memoized version of a function which accepts a single argument
    Languages
  • Squash ObjectImplement a function that returns a new object after squashing the input object into a single level of depth
    Languages
  • Unsquash ObjectPremiumImplement a function that reconstructs a nested object from dot-delimited keys
    Languages
  • getElementsByClassNameImplement a function to get all DOM elements that contain the specified classes
    Languages
  • Table of ContentsPremiumImplement a function to construct a table of contents from an HTML document
    Languages
  • CurryImplement a function that converts a multi-argument function into a chain of unary calls
    Languages
  • HTML SerializerImplement a function to serialize an object into an HTML string with indentation
    Languages
  • JSON.stringifyImplement a function that converts a JavaScript value into a JSON string
    Languages
  • JSON.stringify IIPremiumImplement a function that converts a JavaScript value into a JSON string
    Languages
  • Promise MergePremiumImplement a function to merge the results of two promises into a single value
    Languages
  • useTimeoutImplement a hook that invokes a callback function after a specified delay
    Languages
  • useCountdownPremiumImplement a hook that manages a countdown
    Languages
  • ClampPremiumImplement a function that restricts a number to an inclusive range
    Languages
  • Function LengthPremiumImplement a function that returns the number of parameters expected by a function
    Languages
  • Make CounterImplement a function that accepts an integer value and returns a function that can be repeatedly called to return increasing values
    Languages
  • MeanImplement a function that finds the mean of the values inside an array
    Languages
  • Number of ArgumentsPremiumImplement a function that returns the number of arguments it was called with
    Languages
  • SleepPremiumImplement a function that pauses for a specified duration before resuming execution
    Languages
  • Array.prototype.squarePremiumImplement a custom Array.prototype.square() method that squares the values in an array
    Languages
  • Cancellable TimeoutPremiumImplement a function that acts like setTimeout but returns a function to cancel the pending callback
    Languages
  • ChunkPremiumImplement a function that creates an array of elements split into smaller groups of a specified size
    Languages
  • CompactPremiumImplement a function that creates an array with all falsy values removed
    Languages
  • DifferencePremiumImplement a function that returns the values from one array that are not present in another
    Languages
  • Drop Right WhilePremiumImplement a function that excludes elements from the end of an array until the predicate returns false
    Languages
  • Drop WhilePremiumImplement a function that excludes elements from the beginning of an array until the predicate returns false
    Languages
  • FillPremiumImplement a function that fills an array with values within specified indices
    Languages
  • Find IndexPremiumImplement a function that returns the index of the first element in the array that satisfies the provided testing function
    Languages
  • Find Last IndexPremiumImplement a function that returns the index of the last element in the array that satisfies the provided testing function
    Languages
  • From PairsPremiumImplement a function that returns an object composed from key-value pairs
    Languages
  • Function.prototype.applyPremiumImplement the Function.prototype.apply() function that calls the function with a given `this` value and arguments as an array
    Languages
  • Function.prototype.callImplement the Function.prototype.call() function that calls the function with a given `this` value and provided arguments
    Languages
  • GetPremiumImplement a function to safely access a nested value in an object
    Languages
  • In RangePremiumImplement a function to check if a number falls between two numbers
    Languages
  • IntersectionPremiumImplement a function that returns the unique values present in every array
    Languages
  • Max ByPremiumImplement a function that finds the maximum element based on the specified criteria
    Languages
  • Min ByImplement a function that finds the minimum element based on the specified criteria
    Languages
  • Object MapPremiumImplement a function to transform values within an object
    Languages
  • OncePremiumImplement a function that accepts a callback and restricts its invocation to at most once
    Languages
  • Promise.rejectPremiumImplement a function to return a Promise object rejected with a reason
    Languages
  • RangePremiumImplement a function that returns a sequence of numbers in ascending order
    Languages
  • Range RightPremiumImplement a function that returns a sequence of numbers in descending order
    Languages
  • SingletonPremiumImplement a Singleton class that ensures a class has only one instance while providing a global point of access to that instance
    Languages
  • Unique ArrayPremiumImplement a function to remove all duplicate values from an array
    Languages
  • Array.prototype.atPremiumImplement the Array.prototype.at() method
    Languages
  • Array.prototype.mapPremiumImplement the Array.prototype.map() method
    Languages
  • Array.prototype.reduceImplement the Array.prototype.reduce() method
    Languages
  • CycleImplement a function that takes one or more values and returns a function that cycles through those values each time it is called
    Languages
  • Function.prototype.bindPremiumImplement the Function.prototype.bind() function that creates a new function with the `this` keyword set to a provided value
    Languages
  • SizePremiumImplement a function that returns the size of a collection
    Languages
  • SumPremiumImplement a function that sums numbers by accepting a number and allows for repeated calling with more numbers until it is not called with any number
    Languages
  • Type Utilities IIImplement utilities to determine non-primitive variable types in JavaScript
    Languages
  • useBoolean IIPremiumImplement an optimized version of the useBoolean hook
    Languages
  • useClickAnywhereImplement a hook that handles click events anywhere in the window
    Languages
  • useEffectOncePremiumImplement a hook that runs an effect only once
    Languages
  • useFocusPremiumImplement a hook that enables programmatic focusing of an element
    Languages
  • usePreviousPremiumImplement a hook that returns the previous value of a state
    Languages
  • useTogglePremiumImplement a hook that manages a boolean toggle state
    Languages
  • NEW
    Reading OrderImplement a function that sorts a 2D canvas elements in reading order
    Languages
  • Make Counter IIPremiumImplement a function that returns a counter object with methods to retrieve and manipulate the value
    Languages
  • Array.prototype.concatPremiumImplement the Array.prototype.concat() method
    Languages
  • Count ByImplement a function that counts how many array elements map to each key
    Languages
  • Group ByPremiumImplement a function that groups array values based on a function
    Languages
  • Intersection ByPremiumImplement a function that returns an array of unique values that are included in all given arrays based on a provided iteratee function
    Languages
  • Intersection WithPremiumImplement a function that computes the intersection of arrays using a comparator function
    Languages
  • Is EmptyPremiumImplement a function to check whether a value should be treated as empty
    Languages
  • LimitPremiumImplement a function that accepts a callback and restricts its invocation to at most N times
    Languages
  • Promise.resolvePremiumImplement a function to resolve a given value to a Promise
    Languages
  • Promise TimeoutPremiumImplement a function that resolves a promise if it is fulfilled within a timeout period and rejects otherwise
    Languages
  • Promise.withResolversPremiumImplement a function that returns an object containing a new `Promise` object and two functions to resolve or reject it
    Languages
  • TurtlePremiumImplement a Turtle class that moves a turtle on a 2D plane
    Languages
  • Union ByPremiumImplement a function that creates an array of unique values, in order, from all given arrays.
    Languages
  • useBreakpointPremiumImplement a hook that returns the current breakpoint name based on the current window width
    Languages
  • useClickOutsidePremiumImplement a hook that detects clicks outside of a specified element
    Languages
  • useDebounceImplement a hook that debounces a value
    Languages
  • useEventListenerPremiumImplement a hook that subscribes to browser events
    Languages
  • useHoverPremiumImplement a hook that tracks whether an element is being hovered
    Languages
  • useIntervalPremiumImplement a hook that creates an interval that invokes a callback function at a specified delay
    Languages
  • useKeyPressPremiumImplement a hook that subscribes to keyboard events
    Languages
  • useObjectPremiumImplement a hook that manages an object value
    Languages
  • useSetImplement a hook that manages a JavaScript set
    Languages
  • useStepPremiumImplement a hook that manages a step counter for a multi-step process
    Languages
  • useThrottlePremiumImplement a hook that throttles a value
    Languages
  • useWindowSizeImplement a hook that returns the current height and width of the window
    Languages
  • Camel Case KeysPremiumImplement a function to convert all the keys in an object to camel case
    Languages
  • Compact IIPremiumImplement a function that returns an object with all falsey values removed
    Languages
  • Conforms ToPremiumImplement a function that checks if object conforms to source
    Languages
  • NEW
    Console Log HistoryPremiumImplement a function that intercepts console.log() calls and records the arguments it was invoked with
    Languages
  • Curry IIPremiumImplement a function that transforms a function that takes multiple arguments into a function that can be repeatedly called with any number of arguments
    Languages
  • Data MergingImplement a function to merge rows of data from the same user
    Languages
  • Event Emitter IIPremiumImplement a class that can subscribe to and emit events that trigger attached callback functions. Subscription objects are returned and can unsubscribe itself
    Languages
  • getElementsByTagNamePremiumImplement a function to get all DOM elements that match a tag
    Languages
  • Identical DOM TreesPremiumImplement a function to determine if two DOM trees are the same
    Languages
  • jQuery Class ManipulationPremiumImplement a set of jQuery-like functions that manipulates classes on a DOM element
    Languages
  • NEW
    Lowest Hiding ElementImplement a function to find the deepest DOM element that would hide a set of elements
    Languages
  • MiddlewaresImplement a function that composes asynchronous middleware functions like in Koa.js
    Languages
  • NEW
    Node RegistryPremiumImplement a registry that stores values keyed by DOM nodes
    Languages
  • Promise.allSettledPremiumImplement the Promise.allSettled() function that resolves to an array of outcomes when all the input elements are either resolved or rejected
    Languages
  • Resumable IntervalPremiumImplement a function that creates a resumable interval object
    Languages
  • Text SearchPremiumImplement a function to highlight text if a searched term appears within it
    Languages
  • Undo / Redo ManagerImplement a class that manages value history with undo and redo functionality
    Languages
  • useIdlePremiumImplement a hook that detects user inactivity
    Languages
  • useInputControlImplement a hook that manages a controlled input value and tracks its dirty & touched state
    Languages
  • useMediaQueryImplement a hook that subscribes and responds to media query changes (e.g. screen size, resolution, orientation, etc.)
    Languages
  • useMediatedStateImplement a hook that is similar to useState, but supports a mediation process
    Languages
  • Deep MapPremiumImplement a function to recursively transform values
    Languages
  • NEW
    SpreadsheetImplement a spreadsheet class with numeric cells and addition formulas
    Languages
  • NEW
    Styled Text RangesImplement a function that slices styled text represented as text plus style ranges
    Languages
  • NEW
    Test RunnerImplement a tiny test runner that registers tests, executes them, and summarizes the results
    Languages
  • Text Search IIPremiumImplement a function to highlight text if searched terms appear within it
    Languages
  • Deep MergePremiumImplement a function that merges two objects together
    Languages
  • Memoize IIPremiumImplement a function that returns a memoized version of a function which accepts any number of arguments
    Languages
  • NEW
    Mini Object-relational MapperImplement a simplified Prisma-like in-memory Object-relational Mapper (ORM) with model delegates and basic CRUD operations
    Languages
  • NEW
    Reading Order IIPremiumImplement a function that groups misaligned 2D canvas elements into rows before sorting
    Languages
  • NEW
    Spreadsheet IIPremiumImplement a spreadsheet class with left-to-right arithmetic formulas
    Languages
  • NEW
    Styled Text Ranges IIPremiumImplement a function that overwrites the style of a text range and normalizes the result
    Languages
  • NEW
    Test Runner IIImplement a tiny test runner with nested suites and full spec names
    Languages
  • NEW
    Test Runner IIIPremiumImplement a tiny test runner with inherited setup and cleanup hooks
    Languages
  • Undoable DatabasePremiumImplement a class that manages users with CRUD operations and undo/redo history
    Languages
  • NEW
    Mini Object-relational Mapper IIPremiumImplement a simplified Prisma-like in-memory Object-relational Mapper (ORM) with richer filtering and sorting
    Languages
  • Curry IIIPremiumImplement a function which transforms a function which takes variadic arguments into a function that can be repeatedly called with any number of arguments
    Languages
  • getElementsByTagNameHierarchyPremiumImplement a function to get all DOM elements that match a tag hierarchy
    Languages
  • NEW
    Test Runner IVPremiumImplement a tiny test runner with async specs and async hooks
    Languages
  • Deep Clone IIPremiumImplement a function that performs a deep copy of a value, but also handles circular references
    Languages
  • NEW
    Mini Object-relational Mapper IIIPremiumImplement a simplified Prisma-like in-memory Object-relational Mapper (ORM) with select and relation includes
    Languages
  • NEW
    Rich Text to HTMLPremiumImplement a function that converts styled text ranges into a canonical HTML string
    Languages
  • NEW
    Spreadsheet IIIPremiumImplement a spreadsheet class with dependency-aware updates and cycle detection
    Languages
  • NEW
    Styled Text Ranges IIIPremiumImplement a function that replaces a text range and keeps the style ranges normalized
    Languages
  • NEW
    Styled Text Ranges IVPremiumImplement a function that patches shallow style objects over a text range
    Languages
  • Undo / Redo Manager IIImplement a class that manages draft values and batch-based undo/redo checkpoints
    Languages
  • Undoable Database IIPremiumImplement a class that manages draft user state and batch-based undo/redo checkpoints
    Languages