← Back to JavaScript, page 7

javascript-0124 · Easy fresher

How do you solve FizzBuzz, reverse a string, count vowels, and find the largest number in an array, and what do interviewers look for?

These warm-up questions test loops, conditions, the modulo operator, string and array methods, and clear thinking about edge cases. For FizzBuzz, check the combined multiple of 15 first, or build the word from parts. For strings, remember that they are immutable and spread by code point. For the largest number, use a loop or Math.max with spread, and handle empty arrays.

Full answer

Warm-up problems are short, and interviewers watch how you work: do you ask about the input, handle edge cases, and write readable code?

FizzBuzz: print numbers from 1 to n, but "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for both. The classic mistake is to test the multiple of 3 first, so 15 prints "Fizz". Either test i % 15 === 0 first, or build the output: start with an empty string, add "Fizz" if divisible by 3, add "Buzz" if divisible by 5, and use the number if the string is still empty. The second form extends cleanly to more rules.

Reverse a string: strings are immutable, so produce a new one. [...text].reverse().join('') works for most text, because spread walks code points, while split('') splits surrogate pairs in emoji and breaks them. A loop that builds from the end works too. Mention that perfect handling of combined characters needs Intl.Segmenter.

Count vowels: lowercase the text, then count matches against a Set of vowels, or use match(/[aeiou]/g), which returns null when there is no match, so use ?.length ?? 0.

Largest number: Math.max(...numbers) is short but throws a RangeError for very large arrays (the spread passes every element as an argument), and returns -Infinity for an empty array. A reduce or a loop is safe for any size. Decide what to return for empty input: undefined, null, or an error.

What interviewers look for: correct edge cases (empty input, negative numbers, one element), readable names, no unneeded work, and the ability to explain the time complexity, which here is O(n) for each problem.

javascript
function fizzBuzz(n) {
  const out = [];
  for (let i = 1; i <= n; i++) {
    let word = '';
    if (i % 3 === 0) word += 'Fizz';
    if (i % 5 === 0) word += 'Buzz';
    out.push(word || String(i));
  }
  return out;
}

const reverseString = (text) => [...text].reverse().join('');

function countVowels(text) {
  const vowels = new Set('aeiou');
  let count = 0;
  for (const ch of text.toLowerCase()) if (vowels.has(ch)) count++;
  return count;
}
const countVowelsRegex = (text) => text.match(/[aeiou]/gi)?.length ?? 0;

function largest(numbers) {
  if (numbers.length === 0) return undefined;
  let best = numbers[0];
  for (const n of numbers) if (n > best) best = n;
  return best;
}

console.log(fizzBuzz(15).join(' '));
console.log(reverseString('hello'), JSON.stringify(reverseString('')), reverseString('a\u{1F600}b') === 'b\u{1F600}a');
console.log(countVowels('Interview Questions'), countVowelsRegex('Interview Questions'), countVowelsRegex('xyz'));
console.log(largest([3, -1, 7, 7, 2]), largest([-5, -2, -9]), largest([]), Math.max(), Math.max(...[]));
try {
  Math.max(...new Array(200000).fill(1));
  console.log('spread of 200000 items worked in this engine');
} catch (error) {
  console.log('Math.max with a huge spread:', error.name);
}
console.log(largest(new Array(200000).fill(1)));

Run with Node.js v24.22.1 as js-124.js; two runs gave identical output. Output: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz olleh "" true 8 8 0 7 -2 undefined -Infinity -Infinity Math.max with a huge spread: RangeError 1

Key points

  • FizzBuzz: test the multiple of 15 first, or build the word from parts.
  • Strings are immutable; reverse with spread and join, not split with an empty string, for emoji.
  • match returns null when nothing matches; guard it.
  • Math.max with spread fails for huge arrays and gives -Infinity for empty ones; use a loop or reduce.
  • Show edge cases and complexity; all four are O(n).

Common mistakes

  • Testing for multiples of 3 before testing for multiples of 15.
  • Reversing with split('') and breaking emoji.
  • Calling .length on the null result of match.
  • Using Math.max with spread on very large arrays, or on an empty array without a plan.

Follow-ups

  • How would you add a rule such as 7 to FizzBuzz without a long if chain?
  • How would you reverse the words of a sentence but not the letters?
  • Why does Math.max of nothing return minus Infinity?

Tips

  • Mention edge cases before you write code.
  • Choose clear names and a short, readable solution.
  • State the complexity even for easy problems.