DEV Community

Tahsin
Tahsin

Posted on

A question during my interview for a Front End Developer Position and my solution

Hello dear friends!

So! Recently, I have been looking for new job opportunities and applying through LinkedIn, and then a company reached me and provide a test on Hackerrank which consists of 4 different questions. 2 of them are related HTML/CSS and dom manipulation, and the other 2 are about algorithmic thinking with programming language of your choice.(It would be obvious advantage to choose Javascript, I guess.( duh? ))

Enough with chit-chat and gibberish talking, I am now going to trying to demonstrate and explain a bit my simplistic naive solution.

Essentially, the question asks to sort an array based on both its values and number of occurrences of the elements.

For example;

//If the array is like this:
[4,5,6,5,4,3]
//You need to return something like this:
[3,6,4,4,5,5]

Firstly, I was thinking that I would be creating an object to hold the numbers as keys and their occurrences as values.

//To hold the frequency info for each element within items
let frequency = {};

 items.forEach((value) => { 
        if ( value in frequency ){
            //Once you run into the number again, 
            //increment the respective elements' value of the frequency object
            frequency[value] = frequency[value] + 1;
       }
        else{
            //If you run into number for the first time
            frequency[value] = 1;
       }
    });

After that, I tried checking each entry of the frequency object and then sort the elements based on the number of occurrences.

//Create a 2D array based on 
//the number of occurrences of the elements, e.g. make the object iterable

  const sortedValues = Object.entries(frequency).sort((a,b)=>{
      return a[1]-b[1]
  })

Yes, but right now I have one weird looking two dimensional array, in which, each element is also an array that holds the again the information about frequency. So, I just created another array which is going to be filled based on the numbers' occurrences.

// Here, for each element of our 'sortedValues' 2D array,
// I populate the element as many as of its number of occurences,
//then creating a flattened array at the end(the part with reduce and concat)
const sortedArr = sortedValues.map(sortedValue => 
[...Array(sortedValue[1]).fill(sortedValue[0])]).reduce((a, b) => a.concat(b), [])

So, the resulting array that I acquire after this step is the expected result.

Solution link: https://repl.it/@TahsinYazkan/Sort-an-array-by-frequency-and-value

If you bear with my writing up until this point to finish reading my post with a minimum about of eye-rollings and yikes, thanks a lot!

It really means a lot for me, because this is my first public post on this planet :D and I am talking about some technical subjects for the first time in my life and English is obviously not my mother tongue.

If you have any questions and any problems, shout at me through private message or in the comments.

Top comments (0)