SLCH

2020 - JS interview - later solutions (mine)

Aug 19th, 2020 (edited)
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1)
  2. function findServer(servers, check) {
  3.     const len = servers.length;
  4.     if (len === 0) {
  5.         return Promise.resolve(null);
  6.     }
  7.    
  8.     const srvIndex = len / 2;
  9.     const current = servers[srvIndex];
  10.    
  11.     return check(current).then(checkRes => {
  12.         if (len === 1 && !checkRes) {
  13.             return Promise.resolve(current);
  14.         }
  15.        
  16.         if (checkRes) {
  17.             // Current server is Ok
  18.             // Look to the right
  19.             return findServer(servers.slice(srvIndex, len), check);
  20.         } else {
  21.             // Current server is not Ok
  22.             // Result is current or to the left of current
  23.             // Must confirm that current is first or find one from the left
  24.             return findServer(servers.slice(0, srvIndex), check).then(leftFindServerResult => {
  25.                 if (leftFindServerResult) {
  26.                     // findServer() from left is not null => found result earlier in the array
  27.                     return Promise.resolve(leftFindServerResult);
  28.                 } else {
  29.                     // Nothing found to the left of current, returning current
  30.                     return Promise.resolve(current);
  31.                 }
  32.             })
  33.         }
  34.     });
  35. }
  36.  
  37.  
  38. // 2)
  39. const queue = new Map();
  40. const orderedPrioList = [];
  41.  
  42. let runningPrio = null;
  43. let runningUrls = new Set();
  44.  
  45. function addToQueue(cb, prio) {
  46.     const existing = queue.get(prio);
  47.     let arr = [];
  48.     if (existing) {
  49.         arr = existing.value;
  50.     }
  51.     arr.push(cb);
  52.     queue.set(prio, arr);
  53. }
  54.  
  55. function addPrio(prio) {
  56.     if (orderedPrioList.findIndex(prio) !== -1) {
  57.         return;
  58.     }
  59.     orderedPrioList.push(prio);
  60.     orderedPrioList.sort((a, b) => a - b);
  61. }
  62.  
  63. function startNewPrio() {
  64.     if (orderedPrioList.length === 0) {
  65.         return;
  66.     }
  67.  
  68.     if (runningPrio !== null) {
  69.         queue.delete(runningPrio);
  70.         runningPrio = null;
  71.     }
  72.  
  73.     const runningPrio = orderedPrioList.pop();
  74.     const qbList = queue.get(runningPrio);
  75.  
  76.     for(const cb of qbList) {
  77.         cb();
  78.     }
  79. }
  80.  
  81. function load(url, priority) {
  82.     return new Promise((res, reject) => {
  83.         const jobCb = () => {
  84.             runningUrls.add(url);
  85.             try {
  86.                 const data = await loadData(url);
  87.                 res(data);
  88.             } catch (msg) {
  89.                 reject(msg);
  90.             }
  91.             runningUrls.delete(url);
  92.    
  93.             if (runningUrls.size === 0) {
  94.                 startNewPrio();
  95.             }
  96.         };
  97.        
  98.         // runningPrio === null means queue is empty
  99.         if (!runningPrio || runningPrio === priority) {
  100.             runningPrio = priority;
  101.             runningUrls.add(url);
  102.             jobCb();
  103.         } else {
  104.             addPrio(priority);
  105.             addToQueue(jobCb, priority);
  106.         }
  107.     });
  108. }
  109.  
  110.  
  111. // 3)
  112. function camelToSnake(str) {
  113.     return str.replace(/[A-Z]/g, (m) => `_${m.toLowerCase()}`);
  114. }
  115.  
Add Comment
Please, Sign In to add comment