A fun JavaScript interview challenge
Tags: career, codenewbie, javascript
Published: 2020-02-20 04:48:51

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](coderbyte.com), 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:

```javascript
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](https://dev-to-uploads.s3.amazonaws.com/i/y7wobveg6xqf3m21tfy0.png)

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] (https://coderbyte.typeform.com/to/fb7Yk8) 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](https://coderbyte.com/member?promo=janpromo4351&utm_source=Dev.to&utm_medium=Article&utm_campaign=30%25%20Promo) for 30% off our subscriptions and one-time payments.