uber interview

by kpulkit29

JavaScript

/* function getNameById(id, callback) {
  // simulating async request
  const randomRequestTime = Math.floor(Math.random() * 100) + 200

  setTimeout(() => {
    callback("User" + id)
  }, randomRequestTime)
}

function mapLimit(inputs, limit, iterateeFn, callback) {
  let result = [],
    inExecution = 0, executed = 0;
  function completeCallback(taskId) {
    result.push(taskId)
    inExecution--;
    if (result.length === inputs.length) {
      callback(result);
      return
    }
    executeTask();
  }

  function executeTask() {
    if (executed >= inputs.length) {
      return
    }
    iterateeFn(inputs[executed], completeCallback)
    inExecution++;
    executed++;
    if (inExecution < limit) {
    debugger;
      executeTask();
    }
  }

  executeTask(executed);
}
//example:
mapLimit([1, 2, 3, 4, 5], 2, getNameById, (allResults) => {
  console.log("output", allResults) // ["User1", "User2", "User3", "User4", "User5"]
})
 */

/* const input = [
    [0, [7, 3], "abc"],
    [3, [], "pqr"],
    [8, [], "def"],
    [7, [9], "ijk"],
    [9, [], "lmn"]
];



function buildGraph(nodes) {
  let mp = {};
  for(let item of nodes) {
    let [x, folders, name] = item;
    mp[x] = [folders, name];
  }
  console.log(mp);
  return mp;
}

function printPath(name) {
  let graph = buildGraph(input);
  let q = [], path = "";
  q.push(0);
  while(q.length) {
    let node = q.shift();
    console.log(node)
    let [nbrs, folderName] = graph[node][1];
    path= path + "->" + folderName;
    if(name === folderName) {
      return path;
    }
    for(let nbr of nbrs) {
      q.push(nbr);
    }
    
  }
  return "";
}
printPath("lmn"); */

/* class CustomerVisitClass {
  constructor() {
    this.visitMap = {};
    this.firstVisits = [];
  }
  
  postCustomerVisit(customer) {
    this.visitMap[customer] = this.visitMap[customer] || 0;
    this.visitMap[customer]++;
    if(this.visitMap[customer]>1) {
      delete this.visitMap[customer];
      let index =...