Skip to content

Instantly share code, notes, and snippets.

@mkaraoz
mkaraoz / grokking_to_leetcode.md
Created February 13, 2023 22:09 — forked from tykurtz/grokking_to_leetcode.md
Grokking the coding interview equivalent leetcode problems

GROKKING NOTES

I liked the way Grokking the coding interview organized problems into learnable patterns. However, the course is expensive and the majority of the time the problems are copy-pasted from leetcode. As the explanations on leetcode are usually just as good, the course really boils down to being a glorified curated list of leetcode problems.

So below I made a list of leetcode problems that are as close to grokking problems as possible.

Pattern: Sliding Window

func verifySignature(text2sign : String, signature : Data) throws -> Void
{
var error: Unmanaged<CFError>?
let privateKey = try! retrievePrivateKey()
let publicKey = SecKeyCopyPublicKey(privateKey)
let algorithm: SecKeyAlgorithm = .ecdsaSignatureMessageX962SHA512
// Check if selected algorithm suits the key’s capabilities.
guard SecKeyIsAlgorithmSupported(publicKey!, .verify, algorithm) else {
throw error!.takeRetainedValue() as Error
func sign(text2sign : String) throws -> Data {
var error: Unmanaged<CFError>?
let privateKey = try! retrievePrivateKey()
let algorithm: SecKeyAlgorithm = .ecdsaSignatureMessageX962SHA512
guard SecKeyIsAlgorithmSupported(privateKey, .sign, algorithm) else {
throw error!.takeRetainedValue() as Error
}
let data2sign = text2sign.data(using: .utf8)!
func encrypt(text: String) throws -> Void {
let privateKey = try! retrievePrivateKey()
let publicKey = SecKeyCopyPublicKey(privateKey)
let algorithm: SecKeyAlgorithm = .eciesEncryptionCofactorX963SHA256AESGCM
// Check if selected algorithm suits the key’s capabilities.
var error: Unmanaged<CFError>?
guard SecKeyIsAlgorithmSupported(publicKey!, .encrypt, algorithm) else {
throw error!.takeRetainedValue() as Error
func retrievePrivateKey() throws -> SecKey {
// Create a query with key type and tag
let getQuery: [String: Any] = [kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: TAG,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecReturnRef as String: true]
// Use this query with the SecItemCopyMatching method to execute a search
var item: CFTypeRef?
let status = SecItemCopyMatching(getQuery as CFDictionary, &item)
func createECCKeyPair () throws -> Void {
let attributes: [String: Any] =
[kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom, // only supported key type for ECC
kSecAttrKeySizeInBits as String: 521, // supported key lengths: 256, 383 and 521
kSecPrivateKeyAttrs as String:
[kSecAttrIsPermanent as String: true,
kSecAttrApplicationTag as String: TAG]
]
// The key is generated and stored in the default keychain
public void verify(String message, byte[] signedData) throws Exception {
KeyPair keyPair = getKeys();
Signature signature = Signature.getInstance("SHA256withECDSA");
signature.initVerify(keyPair.getPublic());
signature.update(message.getBytes());
boolean verified = signature.verify(signedData);
if (verified) Log.d("_MK", "Signature verified");
else Log.d("_MK", "Verification failed");
}
/**
* Supported signature algorithms:
* https://developer.android.com/reference/java/security/Signature
*/
public byte[] sign(String message) throws Exception {
KeyPair keyPair = getKeys();
Signature signature = Signature.getInstance("SHA256widthECDSA");
signature.initSign(keyPair.getPrivate());
signature.update(message.getBytes());
public void generateKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(
TAG,
KeyProperties.PURPOSE_SIGN)
// Supported curves: [p-224, p-256, p-384, p-521, prime256v1, secp224r1, secp256r1, secp384r1, secp521r1]
.setAlgorithmParameterSpec(new ECGenParameterSpec("secp521r1"))
.setDigests(KeyProperties.DIGEST_SHA256,
public void generateRsaKeyPairForEncrypiton(final String TAG) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(
TAG,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_ECB)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
.build());