Skip to content

Instantly share code, notes, and snippets.

import json
from decimal import Decimal
def build_decimal(string):
return Decimal(string)
print(json.loads(text, parse_float=build_decimal))
# Abstract Factory
from datetime import datetime
class Random8(object):
def __init__(self):
self.set_seed(datetime.now().microsecond % 255 + 1)
def set_seed(self, value):
self.seed = value
def random(self):
@jwyx3
jwyx3 / The Technical Interview Cheat Sheet.md
Created October 18, 2018 18:14 — forked from tsiege/The Technical Interview Cheat Sheet.md
This is my technical interview cheat sheet. Feel free to fork it or do whatever you want with it. PLEASE let me know if there are any errors or if anything crucial is missing. I will add more links soon.

Studying for a Tech Interview Sucks, so Here's a Cheat Sheet to Help

This list is meant to be a both a quick guide and reference for further research into these topics. It's basically a summary of that comp sci course you never took or forgot about, so there's no way it can cover everything in depth. It also will be available as a gist on Github for everyone to edit and add to.

Data Structure Basics

###Array ####Definition:

  • Stores data elements based on an sequential, most commonly 0 based, index.
  • Based on tuples from set theory.
# credit: http://www.cnblogs.com/grandyang/p/6725380.html
class Solution(object):
def shortestDistance(self, maze, start, destination):
"""
:type maze: List[List[int]]
:type start: List[int]
:type destination: List[int]
:rtype: int
"""
q = collections.deque([start])
# credit: http://www.cnblogs.com/grandyang/p/5294255.html
# count[i] = count[i & (i - 1)] + 1 # i & (i - 1)为清掉1个1后的数
# count[0] = 0
class Solution(object):
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
result = [0]
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
# inorder
def inorderTraversal(self, root):
@jwyx3
jwyx3 / copy-list-with-random-pointer.py
Last active July 23, 2017 16:57
linked list related
# 1) use hash to save mapping between old and new nodes
# Definition for singly-linked list with a random pointer.
# class RandomListNode(object):
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None
class Solution(object):
# 132-pattern
class Solution:
# @param {int[]} nums a list of n integers
# @return {boolean} true if there is a 132 pattern or false
# arr[i] < arr[k] < arr[j] given 0 ≤ i < j < k ≤ n-1
def find132pattern(self, nums):
if not nums or len(nums) < 3:
return False
stack, third = [], -sys.maxint
for i in range(len(nums) - 1, -1, -1):
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return -1
start, end = 0, len(nums) - 1
class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
if not nums or k <= 0:
return -1
return self.quickselect(nums, 0, len(nums) - 1, k)