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
@anilpai
anilpai / count_lakes.py
Created June 18, 2024 09:45
Lakes in an island
# Water is represented by 0 and land is represented by 1.
input_grid = [
[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,1,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,1,0,1,1,1,0,0,1,1,1,0,0],
[0,0,1,0,0,0,0,0,1,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]
@anilpai
anilpai / fenwick_tree.py
Created June 13, 2024 19:04
Fenwick Tree
class FenwickTree:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def update(self, index, value):
while index <= self.size:
self.tree[index] += value
index += index & -index
@anilpai
anilpai / DoS2.py
Created June 12, 2024 12:01
Degrees of separation
from collections import deque, defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def add_edge(self, node, neighbour):
self.graph[node].append(neighbour)
self.graph[neighbour].append(node)
@anilpai
anilpai / DoS1.py
Last active June 12, 2024 12:57
Degrees of separation
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def add_edge(self, node, neighbour):
self.graph[node].append(neighbour)
self.graph[neighbour].append(node)
@anilpai
anilpai / rt_curr_arbitrage.py
Created June 5, 2024 21:47
Real Time Currency Arbitrage
import requests
from typing import Tuple, List
from math import log
currencies = ('PLN', 'EUR', 'USD', 'RUB', 'INR', 'MXN')
api_key = 'fe69bd3037f74f699cf2bf8070b44374'
def get_rates(currencies: Tuple[str, ...], api_key: str) -> List[List[float]]:
rates = []
response = requests.get(f'https://openexchangerates.org/api/latest.json?app_id={api_key}')

Identity and Access Management Our customers trust us with some of their most sensitive data, but with thousands of systems and employees around the globe, we've never been systematic about access control. We want to give employees exactly the right set of privileges they need to do their work.

Welcome! You have been appointed the technical lead of a project to build an identity and access management (IAM) system. At a high level, the components are:

• A way of storing access policies (who can do what); • An API to query these access policies; and • An APl to manage these access policies.

Examples of requests that the system should serve

/* 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