LetMyPeopleCode.com

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

Menu
  • About Greg
  • About LMPC
  • 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

  • My first commute into Seattle since COVID
  • Lenovo P12 Review: A Second Screen – With Caveats
  • Defective Quidel QuickVue COVID Tests
  • The Great American Browser Hunt
  • Chrome’s Usability Crime

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • May 2021
  • April 2020
  • March 2020

Categories

  • Apps
  • Games
  • Hardware
  • JavaScript
  • Mobile
  • Productivity
  • Programming
  • Society & Culture
  • Teaching Code
  • Tech Career
  • Uncategorized
  • WebTech
  • Writing

Tag Cloud

browser hunt code sample commute CX game demo hardware review health interview questions j job hunt diary Mac meta-coding OneNote review Windows

©2022 LetMyPeopleCode.com | Theme by SuperbThemes