Skip to content

Instantly share code, notes, and snippets.

View anilpai's full-sized avatar
:octocat:
Coding

Anil Pai anilpai

:octocat:
Coding
View GitHub Profile
/* Priority Queue based Heap implementation in TypeScript */
class BinaryHeap<T> {
private heapArray: T[] = [];
constructor(private lessThan: (a:T, b:T) => boolean) {
}
size() {
return this.heapArray.length;
@anilpai
anilpai / time_series.py
Last active June 26, 2023 12:07
Generate Time Series based on trip intervals data
import heapq as hq
def generate_time_series(trips):
trips.sort(key=lambda x: (x[0], x[1]))
series = {}
minStartTime = []
for trip in trips:
if minStartTime and minStartTime[0] <= trip[0]:
@anilpai
anilpai / references.txt
Last active March 22, 2024 16:28
Grokking Advanced System Design LInks
Dynamo: How to Design a Key-value Store?
Amazon’s Dynamo : https://www.allthingsdistributed.com/2007/10/amazons_dynamo.html
Eventually Consistent : https://www.allthingsdistributed.com/2007/12/eventually_consistent.html
Bigtable : https://research.google/pubs/pub27898/
DynamoDB : https://www.allthingsdistributed.com/2012/01/amazon-dynamodb.html
CRDT : https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type
A Decade of Dynamo : https://www.allthingsdistributed.com/2017/10/a-decade-of-dynamo.html
Riak : https://docs.riak.com/riak/kv/2.2.0/learn/dynamo/
Dynamo Architecture : https://www.youtube.com/watch?v=w96lLsbI1q8
@anilpai
anilpai / rank_node_2.py
Created April 28, 2021 19:42
Computing Rank of a node in a stream
# Computing Rank of a node in a stream
"""Source: https://www.geeksforgeeks.org/rank-element-stream """
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.left_size = 0
@anilpai
anilpai / node_rank.py
Last active May 1, 2021 01:59
Compute current rank of a node in a stream
# Computing current rank of a node in a stream
class Node:
def __init__(self, val, left=None, right=None, left_size=0):
self.val = val
self.left = left
self.right = right
self.left_size = left_size
class Solution:
@anilpai
anilpai / klarna.js
Created March 30, 2021 07:06
Find Duplicate transactions
function findDuplicateTransactions (transactions = []) {
transactions.sort((a, b) => new Date(a.time) - new Date(b.time));
let duplicates = []
let match = []
let src, dst;
while(transactions.length > 1){
src = 0
dst = 1
match = [src]
@anilpai
anilpai / index.ts
Created March 27, 2021 08:08
TypeScript
type Salutation = {greeting: string, name: string}
/**
* @param {Salutation}
* @return {string}
*/
function greet1({greeting, name}: Salutation):string{
return `${greeting}, ${name}`
}
@anilpai
anilpai / running_median.py
Created August 26, 2020 00:49
Running median of an array of numbers (streaming data) using 2 heaps
from heapq import heappush,heappop, heapify,_heapify_max
arr = [11, 8, 9, 6, 20, 5, 7, 14, 12, 3]
# `low` is a max-heap & `high` is a min-heap
low = []
high = []
# Init median to zero
median = 0
@anilpai
anilpai / git_clone_depth=1.txt
Last active February 28, 2020 19:02
git_clone_depth=1.
Another option if you want to keep the repo as is with the new commits you have added since the shallow, initial commit is this: Amend this commit with an interactive rebase.
Start an interactive rebase including the first (root) commit with
git rebase --interactive --root
Change the pick of the initial commit(s) to edit and save & close the file.
If you've cloned the repo with greater depth than 1, you may need to do the same for all of those commits. Or, alternatively, execute fixup for all of these during the interactive rebase.
@anilpai
anilpai / 4queens.py
Last active February 6, 2020 23:06
N=4 , N-Queens Problem
N = 4
def print_board(board):
for i in range(N):
for j in range(N):
print(board[i][j], end=" ")
print()