Skip to content

Instantly share code, notes, and snippets.

Python Cheatsheet — Screening Exercises

Dict

d[k]                            # access (KeyError if missing)
d.get(k)                        # safe access → None
d.get(k, default)               # safe access with default
k in d                          # membership check
d[k] = v # assign
@submarat
submarat / gist:e84ebe67b7400f3b2080c81a7a56b5cd
Created January 13, 2018 02:05
KeyStore.getKey UnrecoverableKeyException
java.security.UnrecoverableKeyException: Failed to obtain information about key
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreSecretKeyFromKeystore(AndroidKeyStoreProvider.java:282)
at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:98)
at java.security.KeyStore.getKey(KeyStore.java:1062)
at com.wallet.crypto.trustapp.util.KS$override.getData(KS.java:145)
at com.wallet.crypto.trustapp.util.KS$override.access$dispatch(Unknown Source:142)
at com.wallet.crypto.trustapp.util.KS.getData(Unknown Source:24)
@submarat
submarat / merge_sort_2.java
Created December 3, 2017 06:20
Merge sort #2
void mergeSort(char[] str, int i, int j) {
if (i < j) {
int middle = i + (j - i)/2;
mergeSort(str, i, middle);
mergeSort(str, middle, j);
merge(str, i, middle, j);
}
}
public void mergeSort(char[] str, int i, int j) {
while (i < j) {
int middle = i + (j - i)/2;
mergeSort(str, i, middle);
mergeSort(str, middle, j);
merge(str, i, middle, j);
}
BigInteger nTokens = new BigDecimal(tokenAmount).multiply(BigDecimal.valueOf((long)Math.pow(10, decimals))).toBigInteger();
List<Type> params = Arrays.<Type>asList(new Utf8String(to), new Uint256(nTokens));
List<TypeReference<?>> returnTypes = Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() { });
Function function = new Function("transfer", params, returnTypes);
String encodedFunction = FunctionEncoder.encode(function);
String password = PasswordManager.getPassword(from, mAppContext);
@submarat
submarat / gist:ccdda6cf776737158a75efabf3f4cb89
Created November 1, 2017 00:59
Interview question: binary tree bottom-up level order traversal
//Given a binary tree, return the bottom-up level order traversal of the values.
//Additional requirement: return list of lists with respective nodes inside
class Node {
Node left;
Node right;
Object data;
}
List<List<Object>> returnButtomUp(Node tree) {
aa3cc54d7f10fa3a1737e4997ba27c34f330ce16
#include <iostream>
#include <set>
#include <string>
using namspace std;
void swap(string& input, int i, int j) {
if (i < j && i >= 0 && j < input.size()) {
char c = input[i];
input[i] = input[j];
@submarat
submarat / contacts.py
Last active November 29, 2016 17:21
Implementation of the Trie datastructure and solution of "contacts" question on hackerrank.
import fileinput
from trie import Trie
operation_types = ["add", "count", "get"]
def contacts_test():
line_num = 0
op_count = 0
trie = Trie()
for line in fileinput.input():
if line_num == 0:
@submarat
submarat / hashmap.py
Last active July 23, 2017 18:52
Hashmap
# A Hashmap implementation with hash collision resolution
class HashMap:
n_buckets = 100
def __init__(self):
self.buckets = HashMap.n_buckets*[[]]
self.size = 0
@staticmethod