coding questions Flashcards
fib recursive vs iter
0 and 1 as special case , start iterating at 2, in python in range(2,n+1) as the last one is not counted
reverse linked list
use 3 pointers, current and previous, tmp is set not to lose the next. initiatialize previous with None, while on tmp(next), at the end of the loop do the last reversal by hand and return the lat element to print it right
test for anagram
test for length first exit
add frequency by letter in hash on first string
decrement frequency on second string
should be all zero at the end
in python there is a Counter data structure which is a specialized hash that count frequency in strings
test for balance parentheses, one kind, many kinds
one kind simple counter increment decrement
several kind assume proper scoping push open, pop closed, one stack per type of parenthesis, otherwise will allow crossing which is not standard syntax, should be empty at the end
stock max draw
brute force all pairs
maintain index of buy, sell, and min, all at zero initially
always keep the profit as invariant of buy/sell
if new min is found change
keep on testing profit from new min, if over, change the current buy/sell and profit
find the first non repeating character in a string
put character is a fixed size array with their frequency and position, get min of 1s
reverse a string in place
swap from both end, test for odd vs. even, if immutable go from the end
reverse a text
reverse the whole string, then reverse each word, 2 passes
test for palindrome
walk from both end, test of even vs. odd
If I give you coins of denominations {3, 7, 9} (a coin worth 3 cents, a coin worth 7 cents, etc.), can you tell me the minimum number of coins that are needed to make a total of 37? You can assume that an infinite supply of all these coins are available to you.
instance of knapsack problem, the general knapsack problem is NP Complete, no better solution than compute all combination, greedy also, dynamic programming means precomputing solution on smaller sizes, need to keep an array , use lots of memory
explain first classic PK crypto and trapdoor (RSA)
asymmetry based on the fact that multiplying to very large prime is easy but finding the factorization is NP_complete cannot be computed in a reasonable time. The multiplication is the public key, the 2 prime the private key that can be used to decrypt
smallest or biggest number made of same digit
just programming challenge, break it down in list of digit as powers of 10, sort in ascending or descending order and reconstruct by power of 10.
how to compute all subsets of a list/array a.k.a Powerset
used a binary representation bit position turn into index in the list/array. go from 0 to 2^n. Or recursive definition, slicing one element is sub problem with n -1
recursive method, or bottom up starting with 2,3,…n binomial coefficients but best using bit encoding with power set cardinality 2^n
how to compute parity bit of long string
do it byte of word at a time, shift and mask with 1 a count mod 2. Cache each word in a hash
given a string of n char, write a function that computes the maximum number of ‘a’ can be added anywhere without creating any sequence ‘aaa’
go through the string if not a, add ‘aa’ in front end after, if ‘a’ ad just another ‘a’. start and end with ‘aa’