Skip to content

Instantly share code, notes, and snippets.

View kevinwucodes's full-sized avatar

Kevin Wu kevinwucodes

View GitHub Profile
@kevinwucodes
kevinwucodes / readme.md
Last active February 13, 2024 18:32
daily coding problem #33: Compute the running median of a sequence of numbers

Good morning! Here's your coding interview problem for today.

This problem was asked by Microsoft.

Compute the running median of a sequence of numbers. That is, given a stream of numbers, print out the median of the list so far on each new element.

Recall that the median of an even-numbered list is the average of the two middle numbers.

For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should print out:

@kevinwucodes
kevinwucodes / koa-server.md
Last active December 28, 2023 11:31
understanding koajs middleware architecture stack order

Notes

Koa middleware cascade in a more traditional way as you may be used to with similar tools - this was previously difficult to make user friendly with node's use of callbacks. However with async functions we can achieve "true" middleware. Contrasting Connect's implementation which simply passes control through series of functions until one returns, Koa invoke "downstream", then control flows back "upstream".

The following example responds with "Hello World", however first the request flows through the x-response-time and logging middleware to mark when the request started, then continue to yield control through the response middleware. When a middleware invokes next() the function suspends and passes control to the next middleware defined. After there are no more middleware to execute downstream, the stack will unwind and each middleware is resumed to perform its upstream behaviour.

const Koa = require('koa');
const app = new Koa();
@kevinwucodes
kevinwucodes / readme.md
Last active December 9, 2023 21:12
daily coding problem #29: run-length encoding

Good morning! Here's your coding interview problem for today.

This problem was asked by Amazon.

Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".

Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.

function decode(input) {
@kevinwucodes
kevinwucodes / readme.md
Last active August 2, 2023 10:06
daily coding problem #69: Given a list of integers, return the largest product that can be made by multiplying any three integers.

Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Given a list of integers, return the largest product that can be made by multiplying any three integers.

For example, if the list is [-10, -10, 5, 2], we should return 500, since that's -10 * -10 * 5.

You can assume the list has at least three integers.

@kevinwucodes
kevinwucodes / readme.md
Created February 11, 2019 01:31
daily coding problem #81: Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent

Daily Coding Problem: Problem #81

Good morning! Here's your coding interview problem for today.

This problem was asked by Yelp.

Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.

For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"].

@kevinwucodes
kevinwucodes / readme.md
Last active July 18, 2022 19:05
daily coding problem #28: Given a sequence of words and an integer line length k, return a list of strings which represents each line, fully justified

Good morning! Here's your coding interview problem for today.

This problem was asked by Palantir.

Write an algorithm to justify text. Given a sequence of words and an integer line length k, return a list of strings which represents each line, fully justified.

More specifically, you should have as many words as possible in each line. There should be at least one space between each word. Pad extra spaces when necessary so that each line has exactly length k. Spaces should be distributed as equally as possible, with the extra spaces, if any, distributed starting from the left.

If you can only fit one word on a line, then you should pad the right-hand side with spaces.

@kevinwucodes
kevinwucodes / readme.md
Last active September 22, 2021 17:53
daily coding problem #1: Given a list of numbers and a number k, return whether any two numbers from the list add up to k

This problem was recently asked by Google.

Given a list of numbers and a number k, return whether any two numbers from the list add up to k.

For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

function isAddedNumberInArray(inputAry, n) {
  return (
    inputAry
@kevinwucodes
kevinwucodes / readme.md
Last active October 17, 2020 15:42
daily coding problem #2: Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

This problem was asked by Uber.

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].

function aryTransform(input) {
@kevinwucodes
kevinwucodes / sql.sql
Last active November 5, 2019 17:39
sql - recursive employee hierarchy pinpointing a direct manager (with hierarchical order)
--taken directly from https://blogs.msdn.microsoft.com/simonince/2007/10/17/hierarchies-with-common-table-expressions/
--order logic taken from https://stackoverflow.com/questions/32858774/how-to-order-rows-by-hierarchy
/*
Org chart
----------
matt (1)
kirk (2)
chris (3)
tom (4)
@kevinwucodes
kevinwucodes / readme.md
Last active September 12, 2019 18:31
common utilities cheatcheat

Common Utilities Cheatsheet