loading...

Javascript Cardio

roh_dee profile image Rohan Updated on ・4 min read

Introduction

That's right, every time you start doing heavy exercises with stretching your muscles. That is what you do before taking a coding test, flex your brain a bit. Start from a little warm up and then go for complex logics.

Here is the list of code snippets from various interview coding test question.
This is an incremental blog, with more more stuff being added to it, if a logic is much better in terms of reducing complexity, do let me know in the comments, so we can have different flavours for the same.


Reversing String

You should know about the basic string methods.

  function reverseString(value){
    return value.split('').reverse().join('')
  }

This one tests your knowledge.


Palindrome Check

Using the above reverse method for this, but wait if you pass an integer will the same method work. Be careful to cover all the exceptional cases.

  function isPalindrome(value){
    if(typeof value === 'number'){
      return value === reverseInt(value)
    }
    return value === reverseString(value)
  }

function reverseString(value){
    return value.split('').reverse().join('')
  }
  function reverseInt(value){
    return parseInt(value.toString().split('').reverse().join(''))
  }

This one checks your awareness.


Anagram Check

Check if two different words are made using same letters. eg. str1 = 'below', str2 = 'elbow'

As hard as it sounds the logic becomes easy with use of sort method.

function isAnagram(str1,str2){
    return str1.split('').sort().join() === str2.split('').sort().join()
  }

This one checks your laziness.

“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.”― Bill Gates


FizzBuzz

From 1 to 100, print numbers and replace all the three's with Fizz and five's with Buzz

Pretty straight-forward, create an array from 1 to 100, iterate over them using for in, refrain using native for loops here, even though it looks tempting for use.

  function fizzBuzz(){
    array = new Array(100).fill(100)
    for (i in array){
      array[i] = parseInt(i) + 1
      array[i] = array[i] % 3 === 0 ? 'Fizz' : array[i]
      array[i] = array[i] % 5 === 0 ? 'Buzz' : array[i]
    }
    return array
  }

Many of us might write a for loop or a map function to create an Array from 1 to 100.

This one checks your control over language.


Capitalize First letter of each word-

It is always good to show-off you higher order function knowledge where ever you can. But don't just jump into it without a plan. Always use map instead of for/while loops where ever you can.

  function capitalize(value){
    const result = value.split(' ').map(el=>{
      let word = el.split('')
      word[0] = word[0].toUpperCase()
      word = word.join('')
      return word
    })
    return result.join(' ')
  }

This one tests your adaptation to new trends.


Find the max occurrence of a character in a string

This can have various approaches, the best one would be to create an Object with {'characters' : count} and then iterate over the object to return the character/s.

It's always best to provide the best approaches as using the same object, you can find highest/least used character, count vowels, occurrence of a particular letter.

This graphic will explain you when to use a reduce function ->
https://twitter.com/abhinav9669/status/1247961471574630400?s=20

  function maxCharacter(value){
    let result = value.split('').reduce((acc,el)=>{
      if(acc[el]){
        acc[el]++
      }else{
        acc[el] = 1 
      }
      return acc
    },{})
    maxValue = Object.keys(result).reduce((a,b) => result[a] > result[b] ? a : b)
    return maxValue
  }

This one tests your Approach.


Find longest word in a sentence

Using a similar logic to previous one, now create a key-value pair for the {'word' : length}

  function longestWord(value){
    value = value.replace(/,|\.|\s/g,' ').trim()
    valueObj = value.split(' ').reduce((acc,el)=>{
        acc[el] = el.length
        return acc
    },{})
    let maxValue = 0;
    return Object.keys(valueObj).reduce((acc,el)=>{
      if (valueObj[el] >= maxValue){
        maxValue = valueObj[el]
        acc.push(el)
      }
      return acc
    },[])
  }

Similar to above, this one tests your Approach.


Create chunks of arrays

Not every where you can use map/reduce, sometimes you have to be content with just a while.

Now is the time for you to learn about the accessor and mutator methods. A few examples wherein you will find use of slice and splice together.

  function chunkArray(value =[], chunks = 0){
    chunkArr = []
    while(value.length != 0 ){
      chunkArr.push(value.slice(0,chunks))
      value.splice(0,chunks)
    }
    return chunkArr;
  }

This one tests your handling of complexity.


Flatten Arrays

Everyone has used _.flatten for doing this. If you go deep in understanding the underlying code.

It's very hard get the implementation of a redundant function right the first time. If you get it right on your coding test, nothing like it.

values = [[1,2],[3,[4,5]],[6,[7,[8]]]]
flattenArray(values)
function flattenArray(values=[]){
    return values.reduce((acc,el)=>{
      if(el instanceof Array){
        return acc.concat(flattenArray(el))
      }else{
        return acc.concat(el)
      }
    },[]) 
  }

This one tests your handling of high-level of complexity.


I will keep on updating this blog or a write a new blog with as many examples as I come across that are interesting. Stay Tuned for more.

Posted on by:

roh_dee profile

Rohan

@roh_dee

Javascript Developer, keen interest in Data Science

Discussion

pic
Editor guide