Skip to content

Instantly share code, notes, and snippets.

View TheSithPadawan's full-sized avatar

Esther (EST) TheSithPadawan

  • Canada
View GitHub Profile
@TheSithPadawan
TheSithPadawan / task_scheduler.py
Created April 17, 2022 01:47
CFLT onsite prep -- task scheduler
# delayed scheduler
"""
write a function schedule(task, delay) to schedule a task that will be run after some delay
(1) first normal version
(2) thread safe version
"""
import heapq
import time
class Task:
def __init__(self, name):
@TheSithPadawan
TheSithPadawan / window_key_v2.py
Created April 17, 2022 00:57
CFLT onsite prep -- windowed time map
"""
windowed k, v map
"""
import time
class Node:
def __init__(self, time=0, val=0, key=0):
self.time = time
self.val = val
self.key = key
@TheSithPadawan
TheSithPadawan / window_key_v1.py
Created April 16, 2022 21:54
CLFT onsite prep -- window_key_map
"""
windowed k, v map
first version with t for debug
"""
class Node:
def __init__(self, time=0, val=0, key=0):
self.time = time
self.val = val
self.key = key
self.next = None
@TheSithPadawan
TheSithPadawan / variadic_func.py
Created April 15, 2022 15:23
CFLT onsite prep -- variadic function
# variadic function call
"""
Note:
If a function is marked as isVariadic=true, then the last argument can occur one or more number of times.
Example:
FuncA: [String, Integer, Integer]; isVariadic = false
FuncB: [String, Integer]; isVariadic = true
FuncC: [Integer]; isVariadic = true
FuncD: [Integer, Integer]; isVariadic = true
FuncE: [Integer, Integer, Integer]; isVariadic = false
@TheSithPadawan
TheSithPadawan / phrase_search.py
Last active April 15, 2022 03:11
CFLT onsite prep -- inverted index phrase search
# data modeling {word: {doc_id: {positions}}}
from collections import defaultdict
def build_index(sentences):
index = defaultdict(lambda: defaultdict(set))
for docid, sentence in enumerate(sentences):
wordlist = sentence.split(' ')
for pos, word in enumerate(wordlist):
index[word][docid].add(pos)
return index
@TheSithPadawan
TheSithPadawan / locker.py
Created April 10, 2022 03:56
Amazon Locker OO Design
from enum import ENUM
class LockerSize(ENUM):
# for example.. not necessarily a cube
SMALL = (1, 1, 1)
MEDIUM = (2, 2, 2)
LARGE = (3, 3, 3)
class Locker:
def __init__(self, id, size, **kwargs):
@TheSithPadawan
TheSithPadawan / filesys.py
Created April 3, 2022 18:17
Amazon OO Design Linux Find Command
"""
implemnet linux find command as an api ,the api willl support finding files that has given size requirements and a file with a certain format like
find all file >5mb
find all xml
Assume file class
{
get name()
directorylistfile()
getFile()
"""
OO Design: design data types for Amazon locker and packages
Requirement: Design an optimized algorithm to fit the package in the right locker
"""
import bisect
class Locker:
def __init__(self, x, y , z, **kwargs):
self.length = x
self.width = y
self.height = z
"""
Input: String="oidbcaf", Pattern="abc"
Output: true
Explanation: The string contains "bca" which is a permutation of the given pattern.
"""
"""
general pattern:
make a dict for pattern string
iterate over the right end of the window
# leetcode 30.
def find_word_concatenation(s, words):
result_indices = []
worddict = dict()
wordlen = len(words[0])
i, j = 0, 0
total = 0
for word in words:
if word not in worddict:
worddict[word] = 0