Skip to main content
JavaScript Coding Interview Questions with Solutions
0

JavaScript Coding Interview Questions with Solutions

Master JavaScript coding interview questions with practical solutions and explanations covering strings, arrays, objects, frequency counting, duplicates, sorting, and more.

Read in:

Important JavaScript Array & String Coding Questions for Interviews

JavaScript coding questions are commonly asked in technical interviews to evaluate your understanding of loops, arrays, objects, strings, conditions, time complexity, and problem-solving skills.

Below are 7 frequently asked coding problems with examples, solutions, explanations, and complexity analysis.

1. Count Character Frequency

Problem

Given a string, find how many times each character appears in the string.

Example

Input: "Hello"

Output:

{

  H: 1,

  e: 1,

  l: 2,

  o: 1

}

Another example:

Input:

"Hello welcome to ApnaInsights"

Output:

{

  H: 1,

  e: 3,

  l: 3,

  o: 3,

  ' ': 3,

  w: 1,

  c: 1,

  m: 1,

  t: 2,

  A: 1,

  p: 1,

  n: 2,

  a: 1,

  I: 1,

  s: 2,

  i: 1,

  g: 1,

  h: 1

}

Solution

function countCharacterFrequency(str) {
  const freq = {};
  for (let char of str) {
    freq[char] = (freq[char] || 0) + 1;
  }
  return freq;
}
const str = "Hello welcome to ApnaInsights";
console.log(countCharacterFrequency(str));

How does it work?

We create an empty object called freq to store the character counts.

For every character:

freq[char] = (freq[char] || 0) + 1;

If the character doesn't exist, freq[char] is undefined, so:

(undefined || 0) + 1

becomes:

1

If the character already exists, its count is incremented.

Complexity

  • Time: O(n)
  • Space: O(k), where k is the number of unique characters.

2. Find the First Non-Repeating Character

Problem

Given a string, find the first character that appears only once.

Example 1

Input: "Hello"

Output: H

Example 2

Input: "abca"

Output: b

Example 3

Input: "aabbcdde"

Output: c

Solution

function firstNonRepeatingChar(str) {
  const freq = {};
  // Count character frequency
  for (let char of str) {
    freq[char] = (freq[char] || 0) + 1;
  }
  // Find the first character with frequency 1
  for (let char of str) {
    if (freq[char] === 1) {
      return char;
    }
  }
  return null;
}
console.log(firstNonRepeatingChar("vivek"));

Output

i

How does it work?

We need two traversals.

First, count how many times every character appears:

v → 2

i → 1

e → 1

k → 1

Then traverse the original string again.

The first character whose frequency is 1 is returned.

For "vivek":

v → repeated

i → unique

Therefore:

i

is the answer.

Complexity

  • Time: O(n)
  • Space: O(k)

3. Find the Largest Number in an Array

Problem

Given an array of numbers, find the largest number without using Math.max().

Example

Input: [1, 4, 5, 3, 2]

Output: 5

Solution

function largestNumber(arr) {
  let largest = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > largest) {
      largest = arr[i];
    }
  }
  return largest;
}
const arr = [1, 4, 5, 3, 2];
console.log(largestNumber(arr));

Output

5

How does it work?

Initially, we assume the first element is the largest:

largest = 1

Then compare each element:

4 > 1 → largest = 4

5 > 4 → largest = 5

3 > 5 → false

2 > 5 → false

Finally:

largest = 5

Complexity

  • Time: O(n)
  • Space: O(1)

Interview Tip

Avoid this:

let largest = -1;

because it fails when the array contains only negative numbers.

For example:

[-10, -5, -20]

A safer approach is:

let largest = arr[0];

4. Find the Second Largest Number

Problem

Find the second-largest distinct number in an array without sorting the array.

Example 1

Input: [1, 4, 5, 3, 2]

Output: 4

Example 2

Input: [10, 20, 5, 8]

Output: 10

Example 3

Input: [10, 10, 5, 8]

Output: 8
Solution
function secondLargestNumber(arr) {
  let largest = -Infinity;
  let secondLargest = -Infinity;
  for (let num of arr) {
    if (num > largest) {
      secondLargest = largest;
      largest = num;
    } else if (num > secondLargest && num !== largest) {
      secondLargest = num;
    }
  }
  return secondLargest === -Infinity ? null : secondLargest;
}
const arr = [1, 4, 5, 3, 2];
console.log(secondLargestNumber(arr));

Output

4

How does it work?

We maintain two variables:

largest

secondLargest

Initially:

largest = -Infinity

secondLargest = -Infinity

For:

[1, 4, 5, 3, 2]

The values change approximately like this:

1 → largest = 1

4 → largest = 4, secondLargest = 1

5 → largest = 5, secondLargest = 4

3 → no change

2 → no change

Final result:

largest = 5

secondLargest = 4

Complexity

  • Time: O(n)
  • Space: O(1)

This is better than sorting because sorting would generally require O(n log n) time.

5. Remove Duplicates from an Array

Problem

Given an array, remove duplicate values and return only unique values.

Example 1

Input: [1, 2, 1, 3, 4, 2]

Output: [1, 2, 3, 4]

Example 2

Input: ["a", "b", "a", "c", "b"]

Output: ["a", "b", "c"

Solution Using includes()

function removeDuplicates(arr) {
  const unique = [];
  for (let i = 0; i < arr.length; i++) {
    if (!unique.includes(arr[i])) {
      unique.push(arr[i]);
    }
  }
  return unique;
}
const arr = [1, 2, 1, 3, 4, 2];
console.log(removeDuplicates(arr));

Output

[1, 2, 3, 4]

How does it work?

We maintain a separate array called unique.

For every element, we check:

unique.includes(arr[i])

If it doesn't exist, we add it:

unique.push(arr[i]);

Better Solution Using Set

In a real interview, you can also mention:

function removeDuplicates(arr) {
  return [...new Set(arr)];
}
console.log(removeDuplicates([1, 2, 1, 3, 4, 2]));

Output:

[1, 2, 3, 4]

Set automatically stores only unique values.

Complexity

Using includes():

  • Time: O(n²) worst case
  • Space: O(n)

Using Set:

  • Time: O(n) average
  • Space: O(n)

6. Find the Missing Number

Problem

An array contains numbers from 1 to n, but one number is missing. Find the missing number.

Example 1

Input: [1, 2, 3, 5]

Output: 4

Example 2

Input: [1, 3, 5, 2]

Output: 4

Example 3

Input: [1, 2, 3, 4, 6]

Output: 5

Solution

function findMissingNumber(arr) {
  const n = arr.length + 1;
  const expectedSum = n * (n + 1) / 2;
  let actualSum = 0;
  for (let num of arr) {
    actualSum += num;
  }
  return expectedSum - actualSum;
}
const arr = [1, 3, 5, 2];
console.log(findMissingNumber(arr));

Output

4

How does it work?

We use the mathematical formula:

Sum = n × (n + 1) / 2

For:

[1, 3, 5, 2]

The array should contain numbers from 1 to 5.

Expected sum:

5 × 6 / 2 = 15

Actual sum:

1 + 3 + 5 + 2 = 11

Therefore:

15 - 11 = 4

So the missing number is:

4

Complexity

  • Time: O(n)
  • Space: O(1)

Important Interview Point

The array:

[1, 3, 5, 2]

is missing 4, not 7.

The n value is:

arr.length + 1

because one number is missing.

7. Sort an Array Without Using sort()

Problem

Sort an array in ascending order without using JavaScript's built-in sort() method.

Example 1

Input: [3, 4, 2, 1, 5]

Output: [1, 2, 3, 4, 5]

Example 2

Input: [5, 2, 8, 1, 3]

Output: [1, 2, 3, 5, 8]

Solution

function sortArray(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i] > arr[j]) {
        let temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }
  }
  return arr;
}
const arr = [3, 4, 2, 1, 5];
console.log(sortArray(arr));

Output

[1, 2, 3, 4, 5]

How does it work?

We use two loops.

The outer loop selects an element, and the inner loop compares it with the remaining elements.

Whenever we find a smaller number, we swap the values.

For example:

[3, 4, 2, 1, 5]

Eventually the smaller elements move toward the beginning:

[1, 2, 3, 4, 5]

The swapping logic is:

let temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

Complexity

  • Time: O(n²)
  • Space: O(1)

Quick Revision

#

Coding Problem

Main Concept

Time Complexity

1

Character Frequency

Object / Hash Map

O(n)

2

First Non-Repeating Character

Hash Map + Traversal

O(n)

3

Largest Number

Single Loop

O(n)

4

Second Largest

Two Variables

O(n)

5

Remove Duplicates

Set / Array

O(n) average

6

Missing Number

Mathematical Formula

O(n)

7

Sort Without sort()

Nested Loops

O(n²)

📂 Categories

🏷️ Tags

Version History 3 updates
  1. Aug 8, 2026

    Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason

  2. Aug 8, 2026

    Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason

  3. Aug 8, 2026

    Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason

Discussion