LetMyPeopleCode.com

A blog about software, schmaltz, and monkey-patches for the soul

Menu
  • About Greg Bulmash
  • Greg Talks Tech
  • Privacy Policy
Menu
"The Coding Interview"

The Coding Interview: Updating Inventory

Posted on January 30, 2022January 30, 2022 by Greg Bulmash

This series answers coding interview questions from the Coding Interview Prep collection at freeCodeCamp. You can find more with the interview questions tag.

This is an algorithms problem

We have two 2-dimensional arrays in the form of [[count, "product1"], [count, "product2"],...].

The task is to compare them. Where a product exists in both arrays, add the sum of the two counts and the product name in the final array. If the item is unique to either array, add it to the final array. Return the final array, alphabetically sorted by product name.

Example: updateInventory([[2, "Socks"], [4, "Shoes"]], [[4, "Socks], [6, "Tents"]])

Result: [[4, "Shoes"], [6, "Socks], [6, "Tents"]]

Solution explained

The first thing I did was check whether Array.prototype.indexOf() had an argument for looking deeper than one level. It didn’t. So I created a quick function to do a 2-dimensional indexOf(), but highly constrained to the known format. That might cause some brittleness in the long run, but the tight coupling made sense in this case for readability and efficiency.

Next, I filled a new array (final) with the contents of arr1, because JavaScript passes arrays by reference. What that means is to do this in a functional way, I had to create a copy of the array and perform my changes/adds on it to avoid mutating the incoming arrays and creating unintended side effects.

Then it was simply a matter of checking if each element in arr2 existed in final using the 2-dimensional indexOf() function. If one did, update that element in final with the new count. If not, add the element to it.

Then pass a comparison function to Array.prototype.sort() to sort the array alphabetically on the product names.

Then return final.

Solution

function updateInventory(arr1, arr2) {
  // let's not mutate the arrays - keep it functional
  let final = [...arr1];
  for(let i in arr2){
    let spot = indexOf2d(arr2[i][1], final);
    if(spot !== -1){
      final[spot] = ([final[spot][0] + arr2[i][0], arr2[i][1]]);
    } else {
      final.push(arr2[i]);
    }
  }
    final.sort(function (x, y) {
    let a = x[1].toUpperCase(),
      b = y[1].toUpperCase();
    return a === b ? 0 : a > b ? 1 : -1;
    });

    return final;
}

// 2 dimensional array indexOf (*simplified*)
function indexOf2d(str, arr1){
  for(let i in arr1){
    if(arr1[i][1] === str) return i;
  }
  return -1;
}

 

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Job Hunt Diary 2023: Verizon Is Passive-Aggressive?
  • Job Hunt Diary 2023: Eliciting Responses
  • Job Hunt Diary 2023: And so on, and so on…
  • Job Hunt Diary 2023: Freelancing and warning words
  • Job Hunt Diary 2023: Not all job fairs are created equal

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • May 2021
  • April 2020
  • March 2020

Categories

  • Apps
  • Computing
  • Frameworks and Libraries
  • Games
  • Hacks
  • Hardware
  • JavaScript
  • Mac
  • Mobile
  • Other Art Projects
  • Productivity
  • Programming
  • Projects
  • Python
  • Society & Culture
  • Teaching Code
  • Tech Career
  • Uncategorized
  • WebTech
  • Writing

Tag Cloud

Adobe advertising bad ads browser hunt clown car code sample comcast commute copy writing CX design Developer Relations dev rel evernote game demo google drive hardware review health home office home studio internet interview questions j job hunt job hunt diary Mac magento meta-coding OneNote PEO productivity review speedtest t-mobile Windows xfinity

©2023 LetMyPeopleCode.com | Theme by SuperbThemes