Skip to content

Instantly share code, notes, and snippets.

@adisetiawan
Last active June 11, 2020 02:10
Show Gist options
  • Save adisetiawan/6877b46312f25b2ebba4d243bb9536f2 to your computer and use it in GitHub Desktop.
Save adisetiawan/6877b46312f25b2ebba4d243bb9536f2 to your computer and use it in GitHub Desktop.
interview answer

1

def commit(item) error = nil
  if action_1(item).success() && action_2(item).success() && action_3(item).success() && action_4(item).success() && action_5(item).success()
    # no error, all succeessful
  else
    error = "something went error" 
  end
  return error
end

2

HTTP cookies

3

Caching work as middleware layer between client and server to reduce server call which is usually required long computation. By using cache, client request will get cached data instead of directyl from the server. Cache has invalidation and TTL algorithm to measure how long does cache need to be refreshed with udpated data.

4

def find_max(nums)
  max_num = -(Float::INFINITY)
  nums.each do |num| 
    if num > max_num 
      break
    end
  end
  return max_num
end

5

Javascript solution

const array1 = [1,2,3];
const array2 = [2,2,4,5];
const array3 = array1.concat(array2);
console.log(array3.sort());

6

in sync based programming you can use try catch exception, while in async programing such javascript, you can use Promise with Async\Await

7

in Javascript using event driven architecture with pub/sub pattern, long computation use async\await

import { EventEmitter } from 'events';
const eventEmitter = new EventEmitter();

const myEmitter = new eventEmitter();

const maxSlot = 5;
let availSlot = 5;
let customer = 0;

async function processCust() {
  console.log('please sit');
  //pretend long computation, teller had hard time with customer
  await makeCustHappy();
  //since this is async/await, we're sure this below code will be executed after makeCustHappy finish
  myEmitter.emit('finish');
  console.log('thank you');
}

function sleepTeller() {
  //make sure set default Slot
  availSlot = maxSlot;
  console.log('teller going to sleep..');
}

function wakeUpTeller() {
  if(availSlot == maxSlot) {
    console.log('waking up teller..');
  } else {
    console.log('teller already wake up..');
  }
  
}

//error listener
myEmitter.on('error', (err) => {
  console.error('something went error');
});

//finish process customer event listener
myEmitter.on('finish', (availSlot) => {
  availSlot = availSlot++;
});

//start processing customer event listener
myEmitter.on('start', (availSlot) => {
  if(availSlot == 0) {
    //ask customer to go home
    console.log('queue currently full, please come tomorrow');
  } else {
    //reduce available slot
    availSlot = availSlot--;
    //run process customer function
    processCust();
  }
});

//emit event if there's new customer
if(customer > 0) {
  myEmitter.emit('start');
} else {
  sleepTeller();
}

8

REST API will be use only to accept new customer, all other process such teller status, available queue available via websocket

REST API

const express = require('express');
const app = express();
const PORT = 3000;

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

//to process customer use: domain/?c=1
app.get('/', (req, res) => {
  if(req.query.c == 1) {
    myEmitter.emit('start');
    wss.emit('processing');
    res.json({ msg: 'processing customer, use webscoket for realtime status', status: true });
  } else {
    res.json({ msg: 'hello world' });
  }
  
}
app.listen(PORT, () => {
  console.log(`listening at http://localhost:${PORT}`));
}

Websocket client

// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');

// Connection opened
socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');
});

// Listen for status
socket.addEventListener('message', function (event) {
    console.log('status processing ', event.data);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment