DEV Community

Cover image for A fun JavaScript interview challenge
elisabethgross for Coderbyte

Posted on

A fun JavaScript interview challenge

Hey everyone! Welcome back to Code Review, a series of coding interview challenges and career related content released weekly exclusively on Dev.to. I’m Elisabeth Gross and you might know me from the work I do on Coderbyte, a site dedicated to helping developers of any level get their next engineering job. Or you might be following along with this series as you prep for your next big interview!

My favorite part about writing this series is getting to know you all as a community of driven and excited developers, passionate about leveling up their coding skills. If you want more tips and tricks along with some developer lifestyle content, give me a follow on Instagram @elisabethgross568. I’ll be posting about what coffee shops I love to work at in NYC, some of my favorite playlists to listen to while coding and lot of other fun stuff! I can’t wait to engage with you all more. Alright, enough chit chat - lets jump into the solution for last week’s challenge.

The solution

This solution leverages the fact that anagrams will all have the same letter frequency as each other albeit in a different order. We can use an object to store what letters are in each string and how often they appear. If both strings have the same letter frequency - we have an anagram! Here’s what that looks like in code:

function anagram(str1, str2) {
  // replace all whitespace characters
  const arr1 = str1.toLowerCase().replace(/\s+/g, '').split('')
  const arr2 = str2.toLowerCase().replace(/\s+/g, '').split('')

  const obj = {}
  arr1.forEach(letter => {
    if (!obj[letter]) obj[letter] = 0
    obj[letter]++
  })
  arr2.forEach(letter => {
    if (!obj[letter]) return false
    else obj[letter]--
  })
  for (var letter in obj) {
    if (obj[letter] !== 0) return false
  }
  return true
}

Time complexity

This solution has a time complexity of O(n) because we only have to go through each string one time to build the objects that track the letter frequency. Often you can use objects to keep track of something as a great way to reduce the time complexity of an algorithm. Using objects is cheap because getting and setting keys and values has a time complexity of O(1).

This week’s challenge

You are a programmer that has decided to move out of the city and into the forest for some much needed peace and quiet. You normally get your water from a local well, but it seems to have gone dry. You’ve decided to collect rain water to filter, however, your collection device isn’t flat. Write an algorithm to determine how much rain you can possibly collect at once given n non-negative integers representing an elevation map where the width of each bar is 1.

Example: Given [0,3,0,1,0,0,0,1,0,2] return 12

This can be visualized as follows:

Alt Text

Enjoy!

Our newsletter 📫

We send out a small, feature reveal snippet every time we release something big, so our community is the first to know when we break out something new. Give us your email here and we'll add you to our "first to know" list :)

Exclusive Coderbyte promo

And, as a thank you to our awesome Dev.to community, we'd love to extend a special promo to our readers. Check out this secret checkout page for 30% off our subscriptions and one-time payments.

Top comments (3)

Collapse
 
osde8info profile image
Clive Da

sorry ! but you cant use the words 'fun' and 'interview' in the same sentence

Collapse
 
parsyvalentin profile image
Valentin PARSY

I'm sorry you feel that way, when you find fun in an interview, there is a good chance you're gonna go far with the people doing it ^^

Collapse
 
osde8info profile image
Clive Da

bloody good point ! a fun interview IS one when they give you the job !