JavaScript Interview Programs
30 JavaScript programs asked in SDET and automation interviews, ordered from easy to hard. Each one has a worked example, hints and starter code; sign in to see the reference solution.
Easy (12)
- Check a palindrome string
Return whether the string reads identically in both directions; comparison is case-sensitive and includes spaces.
Easy· Strings ·10 min
- Check whether a number is prime
Return whether n is prime; integers below 2 are not prime.
Easy· Numbers ·10 min
- Check whether two strings are anagrams
Return whether two ASCII strings contain the same characters with the same multiplicities; case and spaces matter.
Easy· Strings ·10 min
- Compute a factorial
Compute n factorial, with 0 factorial defined as 1.
Easy· Numbers ·10 min
- Find a value with binary search
Return the index of target in a strictly increasing integer array, or -1 if it is absent.
Easy· Sorting & Searching ·10 min
- Find the first non-repeated character
Return the first character appearing exactly once, or the empty string if none exists.
Easy· Strings ·10 min
- Find the second-largest distinct value
Return the second-largest distinct integer, or null when fewer than two distinct values exist.
Easy· Arrays ·10 min
- Generate Fibonacci numbers
Return the first n Fibonacci numbers starting with 0, 1.
Easy· Numbers ·10 min
- Keep unique array values in order
Remove duplicate integers, preserving the order of their first appearance.
Easy· Arrays ·10 min
- Move zeroes to the end
Move all zeroes after the nonzero values while retaining the relative order of nonzero values.
Easy· Arrays ·10 min
- Reverse a string
Reverse every character of the supplied ASCII string.
Easy· Strings ·10 min
- Split an array into chunks
Split an integer array into consecutive chunks of size k; the final chunk may be shorter.
Easy· Arrays ·10 min
Medium (13)
- Compress consecutive character runs
Replace each maximal run with its character followed by its decimal count, including counts of one. Input contains only lowercase English letters.
Medium· Strings ·20 min
- Count records by a property
Count objects by their string-valued property named key. Return [value,count] pairs ordered by the first occurrence of each value; treat property names literally, including __proto__.
Medium· Collections ·20 min
- Find the longest common prefix
Return the longest prefix shared by all input strings; return an empty string for an empty list.
Medium· Strings ·20 min
- Find the longest substring without repeats
Return the maximum length of a contiguous substring containing no repeated ASCII character.
Medium· Strings ·20 min
- Find the maximum contiguous subarray sum
Return the largest sum of a nonempty contiguous subarray; return null for an empty array.
Medium· Arrays ·20 min
- Find two indices with a target sum
Return indices [i,j] with i < j whose values sum to target, or [] if none exist. Choose the smallest possible j, then the smallest i.
Medium· Hashing ·20 min
- Flatten a nested JSON array
Flatten a nested array of JSON primitive values into one array in left-to-right order. Preserve null, booleans, strings and numbers as values.
Medium· Recursion ·20 min
- Intersect two arrays without duplicates
Return distinct values present in both arrays, ordered by their first occurrence in the first array.
Medium· Hashing ·20 min
- Merge overlapping intervals
Merge closed integer intervals whenever they overlap or touch at an endpoint. Return disjoint intervals ordered by their starts; adjacent intervals such as [1,2] and [3,4] do not overlap.
Medium· Sorting & Searching ·20 min
- Merge two sorted arrays
Merge two nondecreasing integer arrays into one nondecreasing array, retaining duplicates.
Medium· Two Pointers ·20 min
- Rotate an array to the right
Rotate an integer array right by k positions; k may exceed the array length.
Medium· Arrays ·20 min
- Traverse a matrix in spiral order
Return matrix values in clockwise spiral order, starting at the top-left corner. An empty matrix returns [].
Medium· Matrices ·20 min
- Validate balanced brackets
Return whether a string of brackets (), [] and {} is correctly nested and balanced.
Medium· Strings ·20 min
Hard (5)
- Compare two JSON values deeply
Return whether two JSON values are deeply equal. Object key order is irrelevant, array order matters, and primitive values must have the same type and value.
Hard· Recursion ·35 min
- Compute the Levenshtein edit distance
Return the minimum number of single-character insertions, deletions and substitutions needed to transform the first string into the second. Each operation costs one.
Hard· Dynamic Programming ·35 min
- Compute trapped rain water
Treat the array as unit-width nonnegative bar heights and return the total amount of rain water trapped between bars.
Hard· Two Pointers ·35 min
- Find the maximum in every sliding window
Return the maximum value of each consecutive window of k integers.
Hard· Stack & Queue ·35 min
- Find the minimum covering substring
Return the shortest substring of source containing all characters of target with their required multiplicities. Break ties by earliest start; return an empty string if target is empty or no window exists.
Hard· Sliding Window ·35 min
