-
Notifications
You must be signed in to change notification settings - Fork 48
/
anki-card.csv
We can make this file beautiful and searchable if this error is corrected: Illegal quoting in line 6.
2675 lines (2562 loc) · 327 KB
/
anki-card.csv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# FRONT BACK
What is Hamming Code? In telecommunication, Hamming codes are a family of linear error-correcting codes that generalize the Hamming(7,4)-code, and were invented by Richard Hamming in 1950. Hamming codes can detect up to two-bit errors or correct one-bit errors without detection of uncorrected errors.
What is the square root of 256? 16
2^16 65,536
Using bitwise operations, how would you test that a number is a power of 2? bool isPowerOfTwo = (x & (x - 1);
What does ELF stand for? "Executable and Linkable Format.
It's a common standard file format for executables, object code, shared libraries, and core dumps."
example of a latency device CPU core
example of a throughput device GPU core
What is the Hamming Distance? A number used to denote the number of differences between two binary strings of the same length.
What are the 5 steps of the compiling process? "Lexical Analysis
Parsing
Semantic Analysis
Optimization
Code Generation"
What is parsing? Combining tokens and groups of tokens into a tree structure (a parse tree).
What is lexical analysis? The process of dividing program text into words or tokens.
What is code generation? Producing a translation from a high-level program to assembly code. (Linker and Archiver taker over from here to produce machine code)
4 * 16 64
2 ^ 4 16
2^3 8
2^5 32
2^6 64
2^7 128
2^8 256
2^9 512
2^10 1024
2^11 2048
2^12 4096
2^13 8192
2^14 16,384
2^15 32,768
2^32 4.294 Billion
size of char in bits 8 bits
size of char in bytes 1 byte
size of bool in bits 8 bits
size of short in bits 16 bits (at least), and 16 commonly
size of short in bytes 2 bytes, and 2 bytes commonly
size of int in bits 32 bits commonly, at least 16 bits
size of int in bytes 4 bytes commonly, at least 2 bytes
size of long in bits 32 (at least, 32 commonly), 64 on LP64
size of long in bytes 4 bytes, (at least 4, and commonly 4), 8 on LP64
size of long long in bits 64 bits
size of long long in bytes 8 bytes
size of float in bits 32 bits
size of float in bytes 4 bytes
size of double in bits 64 bits
size of double in bytes 8 bytes
size of wchar_t in bits 16 bits
size of wchar_t in bytes 2 bytes
How many levels in a complete binary tree of size n? floor(1 + log(base2)(n))
How can build heap be done in linear time? "A tree of size n nodes, will have floor(n/2^h) nodes with height >= h.
The last half of nodes will be leaves, so they already satisfy the heap property. No work needs to be done on them.
going bottom-up (ignoring the last n/2 items) and satisfying the heap property one level at a time, each level going up the tree has to do at most 1 operation more than the level below it. But as you go up the tree, higher levels have fewer nodes, so you may be doing more operations, but it happens on fewer number of times.
This resembles a series:
n/2 - height 1: 1 operations
n/4 - height 2: 2 operation
n/8 - height 3: 3 operations
...
going to floor(n/2^h) - height h: h operations
n * (1/2 + 2/4 + 3/8 + 4/16 ....) = n * 1 = n"
How are queues usually implemented? Using a Circular Array or Singly Linked List.
How is a deque usually implemented? Using a Circular Array or Doubly Linked List.
How would you swap 2 integers using only bitwise operations? "a ^= b
b ^= a
a ^= b"
How would you turn ON the 3rd bit from the end in a bitstring? x |= (1 << 2)
How would you turn OFF the 3rd bit from the end in a bitstring? x &= ~(1 << 2);
What is a Binary Search Tree? A binary tree is a data structure where each node has a comparable key and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that node's right subtree.
What is an AVL tree? A BST where the height of every node and that of its sibling differ by at most 1.
What is a red-black tree? "BSTs having red and black links satisfying:
- Red links lean left
- No node has two links connected to it
- The tree has perfect black balance: every path from the root to a null link has the same number of blacks"
What is a splay tree? A self-adjusting binary search tree where recently accessed elements are moved to the root so they are quick to access again.
What is a treap? "A random priority is assigned to every key and must maintain two properties:
-They are in order with respect to their keys, as in a typical binary search tree
-They are in heap order with respect to their priorities, that is, no key has a key of lower priority as an ancestor
O(log N) expected time for all operations, O(N) worst case."
What is typical cache line size? "64 bytes.
-- extra below --
To know the sizes, you need to look it up using the documentation for the processor, afaik there is no programatic way to do it. On the plus side however, most cache lines are of a standard size, based on intels standards. On x86 cache lines are 64 bytes, however, to prevent false sharing, you need to follow the guidelines of the processor you are targeting (intel has some special notes on its netburst based processors), generally you need to align to 64 bytes for this (intel states that you should also avoid crossing 16 byte boundries).
To do this in C or C++ requires that you use aligned_malloc or one of the compiler specific specifiers such as __attribute__((align(64))) or __declspec(align(64)). To pad between members in a struct to split them onto different cache lines, you need on insert a member big enough to align it to the next 64 byte boundery"
What is latency? Latency is the delay from input into a system to desired outcome. The time interval between between a stimulus and response.
What is a y-fast trie? A y-fast trie is a data structure for storing integers from a bounded domain. It supports exact and predecessor or successor queries in time O(log log M), using O(n) space, where n is the number of stored values and M is the maximum value in the domain. The structure was proposed by Dan Willard in 1982 to decrease the O(n log M) space used by an x-fast trie.
What is an x-fast trie? An x-fast trie is a data structure for storing integers from a bounded domain. It supports exact and predecessor or successor queries in time O(log log M), using O(n log M) space, where n is the number of stored values and M is the maximum value in the domain. The structure was proposed by Dan Willard in 1982, along with the more complicated y-fast trie, as a way to improve the space usage of van Emde Boas trees, while retaining the O(log log M) query time.
What is a van Emde Boas tree? "The van Emde Boas tree supports insertions, deletions, lookups, successor queries, and predecessor queries in time O(log log U), where U is the universe of items to store. Items are stored in clusters of size sqrt(U).
The van Emde Boas data structure divides the range {0,...,n−1} into blocks of size sqrt(n), which we call clusters. Each cluster is itself a vEB structure of size sqrt(n). In addition, there is a “summary” structure that keeps track of which clusters are nonempty.
More detail:
A van Emde Boas tree (or van Emde Boas priority queue), also known as a vEB tree, is a tree data structure which implements an associative array with m-bit integer keys. It performs all operations in O(log m) time, or equivalently in O(log log M) time, where M = 2m is the maximum number of elements that can be stored in the tree. The M is not to be confused with the actual number of elements stored in the tree, by which the performance of other tree data-structures is often measured. The vEB tree has good space efficiency when it contains a large number of elements, as discussed below. It was invented by a team led by Dutch computer scientist Peter van Emde Boas in 1975."
What is a compressed trie? It's a trie where the non-branching paths are compacted into a single edge.
What relationship of the keys do you lose with a hash table? The ordering of the keys.
Sed command to take a file separated by spaces, turn spaces into newlines, and then sort it alphabetically. sed 's/ /\n/g' words.txt | sort
Print columns 2, 3, and 6 from the date command. date | awk '{print $2, $3, $6}'
Take a file delimited by : and make it tab-delimited. cat /etc/passwd | sed 's/:/\t/g'
Output a file with line numbers. cat -n somefile
Is quicksort stable? No.
Can quicksort be done in-place? Yes.
Can merge sort be done in-place? No. It requires O(n) space. There is an in-place version?
Is merge sort stable? Yes.
Is insertion sort stable? Yes.
Can insertion sort be done in-place? Yes.
Can selection sort be done in-place? Yes.
Is selection sort stable? No.
Is heap sort stable? No.
Can heap sort be done in-place? Yes.
In what case would perfect hashing be practical? When you don't need to support inserts or deletes. The data is static.
How does perfect hashing handle collisions? It creates a second hash table in the buckets where there are multiple items (k), using a second hash function, and k^2 space. The hash table has two hashing levels. k^2 is chosen because the Markov inequality (birthday paradox) ensures we will not have collisions in bucket.
What is the optimal load factor for a hash table? O(sqrt(n))
What is the expected load factor for a hash table? n/m, where n = items, m = buckets) n/m is also called alpha.
What is the technical running time for operations on a hash table? O(1 + alpha), where alpha is the load factor (n/m). Table doubling operations are amortized.
What is the worst-case search time of perfect hashing? O(1)
What is the worst-case space required for perfect hashing? O(n)
What's the best-case running time of binary search? O(1) - we get lucky and find the element right at the midpoint.
What's the worst-case running time of binary search? O(log n)
What are the downsides of using an adjacency matrix to represent a graph? "Finding all the outgoing edges from a vertex takes O(n) time even if there aren't very many, and the O(n^2) space cost is high for ""sparse graphs,"" those with much fewer than n^2 edges."
When is using an adjacency list expensive? Finding predecessors of a node u is extremely expensive, requiring looking through every list of every node in time O(n + e), where e is the total number of edges, although if this is something we actually need to do often we can store a second copy of the graph with the edges reversed.
When are adjacency lists most useful? Adjacency lists are most useful when we mostly want to enumerate outgoing edges of each node. This is common in search tasks, where we want to find a path from one node to another or compute the distances between pairs of nodes. If other operations are important, we can optimize them by augmenting the adjacency list representation; for example, using sorted arrays for the adjacency lists reduces the cost of edge existence testing to O(log(d+ (u))), and adding a second copy of the graph with reversed edges lets us find all predecessors of u in O(d− (u)) time, where d− (u) is u's in-degree.
What is the space required for a graph using an adjacency list? O(n + e)
What's the maximum unsigned number you can represent with 4 bits? 15
What's the maximum unsigned number you can represent with 8 bits? 255
What's the maximum unsigned number you can represent with 16 bits? 65,535
What's the maximum unsigned number you can represent with 6 bits? 63
What's the maximum unsigned number you can represent with 15 bits? 32,767
What's the maximum signed number you can represent with 4 bits? 7
What's the maximum signed number you can represent with 16 bits? 32,767
What's the maximum signed number you can represent with 8 bits? 127
What's the maximum signed number you can represent with 9 bits? 255
What's the maximum unsigned number you can represent with 32 bits? 4.294 Billion
What's the maximum signed number you can represent with 32 bits? 2.147 Billion
How do you get the logarithm of a number n given a base b? Keep dividing n by b until you get to a number <= 1.
How can you write log(base b)(ac)? log(base b)a + log(base b)c
How can you write log(base b)(a/c)? log(base b)a - log(base b)c
How else can you write log(base b)(a^c)? c * log(base b)a
How you can express log(base b)a as another base? log(base d)a / log(base d)b
Why is log(base2)(2^n) == n? log(base2)(2^n) = n * log(base 2)2 = n * 1 = n
What is the arithmetic series: 1 + 2 + 3 + 4 + ... (n - 1) + n? (n(n+1)) / 2
What is the value of the geometric (exponential) series when x != 1: 1 + x + x^2 + x^3 + ... x^n ? (x^(n + 1) - 1) / (x - 1)
What is the sum of this series when it's infinite and x < 1? 1 + x + x^2 + x^3 + ... x^n ? 1 / (1 - x)
What is the sum of the harmonic series for the nth harmonic number: 1 + 1/2 + 1/3 + 1/4 .. + 1/n "No closed form, only good approximations:
≈ 0.57721
the Euler-Mascheroni constant."
What is the maximum unsigned integer you can represent with n bits? 2^n - 1
Given a fully balanced binary tree with x nodes, what is the height of the tree in nodes? log(base2) x + 1
Given a fully balanced k-ary tree with x nodes, what is the height of the tree in nodes? log(basek) x + 1
A binary tree with height h can contain at most how many nodes? 2^(h+1) − 1 nodes
For a k-ary tree with height h, the upper bound for the maximum number of leaves is: k^h
What is the complexity of Dijkstra's shortest-path algorithm? "O(e log v), where e is the number of edges.
It must scan each edge, and gets and updates values on the heap."
What is a drawback of using an adjacency matrix for an undirected graph? Half of the entries in the matrix are duplicates.
What is the memory needed to store an adjacency list? Theta( |V| + |E| )
What is the memory needed to store an adjacency matrix? Theta(|V|^2)
How would you implement a queue with a linked list? Use a tail pointer. Push new items at the tail, pop items at the head. Both operations are constant-time.
How would you implement a stack with a linked list? Push and pop items at the head. Both operations are constant-time.
What preference of nodes vs leaves does preorder traversal give on a tree? Nodes first, leaves later.
What preference of nodes vs leaves does postorder traversal give on a tree? Leaves first, internal nodes later.
What could you use in DFS to turn a recursive algorithm into an interative one? A stack.
What do you use to keep track of nodes to visit in BFS? A queue.
Using a stack to keep track of unvisited nodes gives what kind of traversal? DFS
Using a queue to keep track of unvisited nodes gives what kind of traversal? BFS
In a highly connected graph of n vertices, how many cycles can there be? (n - 1)! - enumerating is possible (using backtracking), but there will be a lot.
What can use to find if a graph is bipartite? BFS. Using only 2 colors. When you encounter a new vertex, if it has no color, give it the opposite color of its parent vertex. If it is already colored the same, the graph is not bipartite.
How can you find a cycle in a graph? DFS. If you discover an edge that connects to an ancestor (previously discovered vertex), you have a cycle.
What is an articulation vertex? A vertex of a graph whose deletion disconnects the graph.
How can you find an articulation vertex? "DFS multiple times. Remove each edge one at a time, doing a DFS after each, so see if you end up with > 1 connected components. If you remove a node and then DFS and find you have fewer than m - 1 edges, you've deleted an articulation vertex. O(n(n+m))
A faster way, with a little more bookkeeping, can be done in O(n+m) time, if you do DFS and keep track of parents and make a note when you reach a back edge, which connects to an ancestor."
How could you identify errors in a DNA fragment assembly given many pairs of sequences, where item A must appear before B in the larger sequence? Build a DAG representing all the left-right constraints. Any topological sort of the DAG is a consistent ordering. If there are cycles, there must be errors.
What path does BFS find in a graph? The shortest path tree from start to all nodes (unweighted)
What's the upper bound on the number of edges in a graph G(V, E)? |V|^2
In Python, initialize a list of lists called x with 100 elements. x = [[] for _ in range(100)]
What is the optimal substructure property tell us about shortest paths? That a subpath of a shortest path is also a shortest path.
What is a Dunder method? A magic method in Python, such as __getitem__ and __len__.
What is the sum of numbers from 1 to 2^n? "2^(n+1) - 1
The sum of a sequence of powers is roughly equal to the next value in the sequence."
How many ways can you rearrange a string of n unique characters? "n!
Permutations."
How many ways can you arrange k characters from n unique characters? "n! / (n - k)!
Permutation of n elements of size k."
How many subsets (ordering doesn't matter) of size k are there in n unique characters? "n! / k!(n - k)!
This is n choose k."
What should you avoid in your base case in recursion? "Too many base case scenarios. Just have one base case so you can return as quickly as possible. Avoid ""arm's length"" recursion."
What is the bandwidth of a graph? The longest edge in the permutation that gives you the shortest edges.
When talking dynamic programming, what is feasibility? The rules the algorithm must adhere to in reaching its solution.
When talking dynamic programming, what is optimality? An algorithm has optimality if the subsolutions of an optimal solution of the problem are themsleves optimal solutions for their subproblems.
What is dynamic programming? "Dynamic programming is a general-purpose algorithm design technique that is most often used to solve combinatorial optimization problems, where we are looking for the best possible input to some function chosen from an exponentially large search space.
There are two parts to dynamic programming. The first part is a programming technique: dynamic programming is essentially divide and conquer run in reverse: we solve a big instance of a problem by breaking it up recursively into smaller instances; but instead of carrying out the computation recursively from the top down, we start from the bottom with the smallest instances of the problem, solving each increasingly large instance in turn and storing the result in a table. The second part is a design principle: in building up our table, we are careful always to preserve alternative solutions we may need later, by delaying commitment to particular choices to the extent that we can.
The bottom-up aspect of dynamic programming is most useful when a straightforward recursion would produce many duplicate subproblems. It is most efficient when we can enumerate a class of subproblems that doesn't include too many extraneous cases that we don't need for our original problem."
What is the complexity for a naive recursive Fibonacci function? "Θ(φ^n), where phi(φ) is the golden ratio (1 + sqrt(5)) / 2.
approx: 1.618"
What does __getitem__ in a class allow us to use? "- iteration
- slicing
- reverse iteration
- random.choice"
How many subsets are there in n items? 2^n
What is a contiguously-allocated structures, and give examples. Contiguously-allocated structures are composed of single slabs of memory, and include arrays, matrices, heaps, and hash tables.
What are linked data structures and give examples. Linked data structures are composed of distinct chunks of memory bound together by pointers, and include lists, trees, and graph adjacency lists.
What are some benefits of arrays? "- Constant-time access given the index
- Space efficiency
- Memory locality"
Why is memory locality important? Physical continuity between successive data accesses helps exploit the high-speed cache memory on modern computer architectures.
What are some advantages to linked lists over arrays? "- Overflow on linked structures can never occur unless the memory is actually full.
- Insertions and deletions are simpler than for contiguous (array) lists.
- With large records, moving pointers is easier and faster than moving the items themselves."
What are some advantages to arrays over linked lists? "- Linked structures require extra space for storing pointer fields.
- Linked lists do not allow efficient random access to items.
- Arrays allow better memory locality and cache performance than random pointer jumping."
Codeless question: Write a function to find the middle node of a singly-linked list. How would you do it?
Codeless question: Write a function to compare whether two binary trees are identical. Identical trees have the same key value at each position and the same structure. How?
Codeless quesiton: Write a program to convert a binary search tree into a linked list. How would you do it?
Codeless Question: You are given a search string and a magazine. You seek to generate all the characters in search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string. How would you do it?
Codeless question: Give an algorithm for finding an ordered word pair (e.g., “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? "You could use a hash table, creating or updating an entry for each pair.
Keep track of max_frequency and most_frequent_phrase.
Just increment the count, and when you see the new count is > than max_frequency, update max_frequency and most_frequent_phrase"
Codeless question: Given a set of n numbers, how do you find the pair of numbers that have the smallest difference between them? Sort them: Once the numbers are sorted, the closest pair of numbers must lie next to each other somewhere in sorted order. Thus, a linear-time scan through them completes the job, for a total of O(n log n) time including the sorting.
Codeless question: Are there any duplicates in a given set of n items? This is a special case of the closest-pair problem, where we ask if there is a pair separated by a gap of zero. The most efficient algorithm sorts the numbers and then does a linear scan though checking all adjacent pairs.
Codeless question: Given a set of n items, which element occurs the largest number of times in the set? Bonus: How do you find out how many times some element k appears? "If the items are sorted, we can sweep from left to right and count them, since all identical items will be lumped together during sorting.
To find out how often an arbitrary element k occurs, look up k using binary search in a sorted array of keys. Then use binary search in each direction to find where that run of the number begins and ends."
Codeless question: Give an efficient algorithm to determine whether two sets (of size m and n, respectively) are disjoint. "The small set can be sorted in O(m log m) time. We can now do a binary search with each of the n elements in the big set, looking to see if it exists in the small one. The total time will be O((n + m) log m).
This is better than sorting the larger array or sorting both sets and going through the list."
What is a uniform distribution? When a known finite number of outcomes are equally likely to occur. When graphed as a histogram of occurrences, it's a flat line. N items each have 1/n probability.
What is a normal distribution? The standard normal probability density function has the famous bell shape that is known to just about everyone.
What is an n-gram? A set of N sequential words appearing together.
What's the maximum unsigned number you can represent with 9 bits? 511
Whats the average height of a binary search tree after n insertions? "2 ln n
About 39% taller than a perfectly balanced BST"
What is the runtime of randomized quicksort? Theta(n log n) time on any input, with high probability.
Python: Default arguments are okay to use with the following caveat. "Do not use mutable objects as default values in the function or method definition.
Lists are not ok."
Python: Give an example of a conditional expression. x = 1 if cond else 2
Python: break this string 'a/b/c' into a list of ['a', 'b', c'] 'a/b/c'.split('/')
Python: split a word or sentences into characters. list('some words')
What's the difference between foo.sort() and sorted(foo) where foo is a list? "foo.sort() changes the list to its sorted state
sorted(foo) doesn't change foo, just returns a sorted list."
Can you sort a tuple? "Yes, using sorted(foo)
but not with foo.sort()"
Python: How would you remove indices 6 to 11 in a list foo? del foo[6:12]
Python: How can you tell if an index is in a list? if 0 < i < len(foo):
What's the probability of getting heads twice? P(HH) "1/4
= 1/2 * 1/2"
What's the probability of getting heads, tails, heads? P(HTH) "1/8
= 1/2 * 1/2 * 1/2
the ordering of HTH doesn't matter. It could be TTH or HHH or TTT, they are tall the same."
What's the probability of getting heads 200 times in a row? (1/2)^200
What's the Probability of getting head, tails or tails, heads? P(HT U TH) (1/2 * 1/2) + (1/2 * 1/2) = 2/4 = 1/2
What's the probability of getting exactly 3 heads in 8 flips? "56/256 = 7/32
(8 choose 3)/2^8"
You have 26 letters in an alphabet. How many ways you can arrange 3 letters from that alphabet where repeated letters is OK? 26*26*26 = 26^3 = 17,576
You have 26 letters in an alphabet. How many ways you can arrange 3 letters from that alphabet without repeating a letter? 26*25*24 = 26 permute 3 = 15,600
Given an alphabet of 26 letters, how many times you can you choose 3 letters, if ordering of the letters doesn't matter? 26*25*24 / 3! = 2600
What is 0! ? 1
There are 4 people. How many ways can they shake each other's hands without shaking the same person's hand twice? "6
= 4 choose 2
= 4 * 3 / 2"
Right-propagate the rightmost 1-bit, producing all 1's if x = 0 (e.g., 01011000 01011111): x |= (x - 1)
Isolate the right-most bit in x. x & ~(x - 1)
What is P? The set of all problems solvable in polynomial time.
What is EXP? The set of all problems solvable in exponential time.
What is R? (not real numbers) The set of problems solvable in finite time.
Is the Halting Problem in R? No.
What does NP mean? What is an NP algorithm? NP is the set of decision problem solvable in non-deterministric polynomial time. An NP problem can be solved by a lucky algorithm that magically always finds a yes decision. NP problems can be checked in polynomial time.
What makes a problem NP-Complete? "If x is an element of NP and of NP-hard. NP-Complete is the narrow intersection of NP and NP-hard.
Because NP is such a large class of problems, most NP-hard problems you encounter will actually be complete."
What is Fib(0) ? 0
What is Fib(3) ? 2
What is Fib(2) ? 1
What is a PTAS? Polynomial-time approximation scheme.
What is a pointer? A memory address stored in memory.
Give 2 examples of common data structures that contain reference cycles. "- Doubly-linked lists
- Trees with parent and child pointers"
What is a weak reference in Python? "A weak reference to an object does not affect its reference count.
When the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. A primary use for weak references is to implement caches or mappings holding large objects, where it’s desired that a large object not be kept alive solely because it appears in a cache or mapping."
How does garbage collection work in CPython? "CPython uses reference counting and generational garbage collection. There are 3 age stages where objects live in memory. They all start in the ""nursery"", stage0, then if they survive a garbage collection, they are moved to stage1, the oldest objects that continue to survive in stage1 are promoted to stage2. The gc module has thresholds 700, 10, 10 for each stage. In order to decide when to run, the collector keeps track of the number object allocations and deallocations since the last collection. When the number of allocations minus the number of deallocations exceeds threshold0, collection starts. If generation 0 has been examined more than threshold1 times since generation 1 has been examined, then generation 1 is examined as well. Similarly, threshold2 controls the number of collections of generation 1 before collecting generation 2."
What is reference counting? RC is a method of garbage collection. The runtime keeps track of references to an object by manipulating the reference count on each assignment and delete (del), and when the reference count reaches 0 it means the object is practically unreachable. When the next collection runs, the object's memory will be reserved to allocate for new objects.
What is a finalizer in Python? A finalizer is a destructor, named __del__. __del__() is run when the runtime is about to destroy the object.
What are 2 advantages of reference counting? "- easy to implement
- collects garbage incidentally without large pauses in execution."
What are 2 disadvantages of reference counting? "- it cannot collect circular references
- manipulating reference counts at each assignment is very slow."
What is cyclic garbage collection? Detects and removes cycles unreachable by the program.
How is garbage collection done in PyPy? "The GC implementation can be chosen at runtime. It's pluggable.
PyPy uses mark and sweep, and generational gc optimization. Marked objects are promoted from the nursery to an older generation.
PyPy uses incremental garbage collection, where major collection is split into multiple passes, each lasting only a few milliseconds."
What is a pointer? A pointer is a memory address stored in memory.
How does mark and sweep work? (In Java) Perform a DFS on the graph of references to objects. This graph can have multiple roots. Each root is a reference that the program can access directly, such as a variable. Traverse the graph, setting a mark bit in each object. The sweep phase causes unmarked memory to be linked together in a list, so that memory can be reallocated. Sometimes this also triggers compaction, which moves used objects adjacent to each other in memory. The side effect of this is that free memory is also adjacent to free memory so large blocks can be allocated.
What is copying garbage collection (stop and copy)? Heap memory is split into 2 partitions: an old space and a new space. Find live objects by DFS of their reference graph, and move live objects into the new space. The new space is now called the old space. Unreachable objects are simply left in the old space to be overwritten the next time collection occurs. The movement of objects implicitly compacts the objects. Disadvantage: you can only use half of the heap space.
How could you implement an LRU cache? "A fast lookup table, like a hash table or binary tree, and a linked list of items by use. When you access or add an item, you delete it from the linked list and add it to the head of the list. Then to prune, traverse the linked list and remove trailing elements, and delete them from the storage (tree or hash table).
You can also use a splay tree, since it moves accesses to the root. To prune items, somehow find and remove the leaves, since the number of leaves will be about n/2."
What is a direct mapped cache? It's a type of cache used in the CPU, where the lower order bits of a given memory address are used modulo the number of cache lines to place or lookup in the cache. Collisions are treated as overwrites.
What is a fully-associative cache? "It's a type of cache used in the CPU, where lookups are done on all cache lines in parallel to determine a hit or miss.
This requires a very large number of comparators that increase the complexity and cost of implementing large caches. Therefore, this type of cache is usually only used for small caches, typically less than 4K."
Disadvantage of a fully-associative cache? "- expensive due to parallel checks
- complexity of implementing this scheme"
How do some processors handle caching for data and instructions? There will be a slightly slower (3-4 clocks latency) separate cache for data.
What is an N-way set associative cache? A Set-Associative cache scheme is a combination of Fully-Associative and Direct Mapped caching schemes. A set-associate scheme works by dividing the cache SRAM into equal sections (2 or 4 sections typically) called cache ways. The cache page size is equal to the size of the cache way. Each cache way is treated like a small direct mapped cache.
Describe the memory hierarchy of a Core i7 processor. "Inside of each core is a 32 kB L1 instruction cache, a 32 kB L1 data cache (it's 8-way set associative), and a dedicated 256 kB L2 cache (also 8-way set associative).
Outside of the cores is the L3, which is much larger at 8 MB and smarter (16-way associative) than the L2s."
When is entropy at its maximum? When all outcomes are equal.
As predictability increases, what happens to entropy? It decreases.
As it relates to compression, as entropy increases, does our ability to compress increase? No, it decreases.
What does low entropy mean? Very predictable.
What is entropy? The minimum number of bits needed to represent x number of states. Also, information we don't know.
What is a Caesar cipher? The cipher created by shifting/rotating an alphabet by a specific number previously agreed upon.
What is a polyalphabetic cipher? A cipher where each letter is shifted based on the shifts of letters in a key word.
What is the entropy of n equally likely outcomes? log(n)
Is GET idempotent? Yes
Is PUT idempotent? Yes.
Is POST idempotent? No.
Is DELETE idempotent? According to the REST spec, yes, but it's up to the developer to conform to that. It can be achieved by using a deleted flag for a resource instead of completely removing the resource.
What is idempotent? The property that a method has side-effects of making more than one identical requests is the same as for a single request.
What is HMAC? HMAC is a keyed-hash message authentication code used to provide a checksum for a message, sent along with the message to provide confidence that the message has not been tampered.
What is a MAC? MAC is a message authentication code used to provide a checksum for a message, sent along with the message to provide confidence that the message has not been tampered.
How does RSA work? It's a public/private key cryptography method. The public key can be used to encrypt a message into ciphertext that only the owner of the key can decrypt. The owner of the key uses their secret key to encrypt messages, and their secret key to decrypt messages encrypted with their public key.
What is the phi function? It answers the number of integers <= n that do not share a common factor with n.
What is phi(n) if n is prime? n - 1
What is the gcd of a number a and prime p when p is prime? 1, unless a is a pseudoprime (Carmichael number)
What does SHA stand for? One of the family of Secure Hashing Algorithms.
What is the largest output size of SHA-3? 512 bits
What are desirable properties of one-way functions? "- collision resistant
- target collision resistant
- non-malleable"
If a one-way function is collision-resistant, does that mean it's also target collision-resistant? Yes.
If a one-way function is target collision-resistant, does that mean it's also collision-resistant? No.
What is symmetric key encryption? There is a known encryption function, and one key is used to encrypt and decrypt. The key has to be shared between 2 parties.
How does Diffie-Hellman key exchange work? 2 parties agree on a G and a modulus p, and each party comes up with a number. One party does G^a and the other G^b. They pass this information. One party A computes the key from B as B^a mod p. B computes A^b mod p to get the key.
Is Diffie-Hellman key exchange perfect? No. A man in the middle can intercept one side, and communicate with parties A and B independently.
How is RSA (using product of large primes) better than using NP-Complete algorithms for encryption? NP-Complete algorithms are hard in the worst case, but can be sometimes solved in linear time in the average case. Compositing the product of large primes is hard in the average case.
What is Vigenere cipher? "Improvement on Caesar cipher. Letters are shifted based on a shifted dictionary. ""Polyalphabetic cipher"""
What is a one-time pad encryption? "The ""perfect"" simple encryption scheme. Pad/key is the same size as the message being encrypted. The key is randomly generated and xored against the plain text. Or key used to determine the amount each letter should be shifted."
What is block size in cryptography? Symmetric key ciphers are generally divided into stream ciphers and block ciphers. Block ciphers operate on a fixed length string of bits. The length of this bit string is the block size. Both the input (plaintext) and output (ciphertext) are the same length; the output cannot be shorter than the input – this is logically required by the Pigeonhole principle and the fact that the cipher must be invertible – and it is simply undesirable for the output to be longer than the input.
What is the limiting factor of compression? For lossless compression, it's entropy. For lossy compression, it's our acceptance with the amount of loss.
What is LZ* compression based on? Cataloging the positions and lengths of redundant patterns and combining the values with a dictionary.
What is LZMA? It's a variant of LZ77 that uses Markov chains. It's used in the 7z compression algorithms used in 7-zip.
What is DEFLATE? It's an lossless compression algorithm based on LZ77 used in Gzip, WinZip, and mod_deflate, which is bundled with Apache web server for automated gzip compression of HTTP served content. It uses LZ77 and Huffman coding.
How does LZ77-based compression work? "LZ77 is a dictionary encoding algorithm, which is a statistical encoding algorithm. Compression in the LZ77 algorithm is based on the notion that strings of characters (words, phrases, etc.) occur repeatedly in the message being compressed.
The input is partitioned into 2 segments: a search buffer and a look-ahead buffer. The search buffer maxes out at 32KB. Starting with one character in the LA buffer, it looks back in the search buffer to find a copy of the symbol. If one is found, it looks at the second symbol of the LA buffer to see if it also matches the predecessor. Using this method, it can detect long phrases of symbols and encode them as one unit.
This process implicitly creates a rolling statistical probability for each symbol/phrase."
What is Huffman encoding? Huffman encoding algorithm analyzes the occurrence of individual symbols and creates a binary tree where the common symbols are closest to the root, using fewer bits to encode, and less common/rare symbols have longer paths on the tree, with longer encodings to accommodate. By traversing the tree, from root to leaf, and keeping track of 1 or 0 at each node, we can determine the encoding of the symbol.
What is the primary factor of compression? Probability of redundant portions of input.
How can you maximize compression? By deeply analyzing the given input to reduce redundancy as much as possible.
What compression scheme uses Burrows-Wheeler transform? BZip2
What is the Burrows-Wheeler transform? "It's a compression method involving the sorting of all possible rotations of the input text into lexicographic order. Take as output the last column and the index of the row that the original text appears in.
To decode, take the single column and repeatedly add the final columns characters to each of the rows, sorting each time. Once you've reached the length of the column's height, use the index to find the output string."
For Gzip in web servers, what is the usual setting? 6
What is the min and max compression settings in command line gzip? 0-9
How can you make JSON better compressable with Gzip? Transpose from multiple mini-dicts into one dict with arrays as the values. This allows the items in an array to fit within the 32KB search buffer common to LZ-based compression.
What are Dense Codes? A way to put symbols or words into a dictionary or array, and use the indices as the values in the text to save space so that words are not repeated.
What is the LZ in LZ compression? Lempel-Ziv
What is OS hardware virtualization? The abstraction of heterogeneous hardware provided by the operating system, to hide the details of interfacing with various hardware so that they share a common interface for that type.
What is a process? An instance of an executing program consisting of an address space and one or more threads of control. It has restricted rights. It owns a region of memory. It owns file descriptors, file system context. It encapsulates one or more threads sharing the process' resources. It is isolated from other processes.
What is a context switch? The copying out and in of register state to switch from running one process to running another.
What is the scheduler? The scheduler manages the priorities of user and OS processes.
What causes operating systems to crash most of the time? Device drivers. They are in the kernel and very low level. They have access to all the hardware, including memory. They are written by authors outside of the operating system.
What is a process VM? A virtual environment that supports the execution of a single program. This is provided by an operating system.
What is a system VM? A virtual environment that supports the execution of an entire operating system and its applications.
How does the OS abstract the environment for a process? Each process thinks it has all the memory and CPU time, and thinks it owns all devices.
What is fault isolation? Processes are unable to directly impact other processes. Bugs can't crash the whole machine.
What is a thread? A single, unique execution context.
What's the maximum address in memory for a 32 bit processor? 2^32 - 1
What is the execution sequence for a program? "- Fetch instruction
- Decode
- Execute (perhaps using ALU and writing result to a register)
- Write results to registers or memory
- Increment program counter
- Repeat"
What does the program counter point to? An address in memory that contains the current instruction.
What is the stack pointer? A register that holds the address of the top of the stack portion of a process' memory.
What is the heap pointer? A register that holds the address of the top of the heap portion of a process' memory.
What is multiprogramming? Making one processor appear as multiple processors, each handling one process each.
What triggers a context switch? Timer interrupt, hardware interrupt, I/O where we're waiting for a read or write to complete (OS doesn't want to waste time waiting), voluntary yield.
What is dual-mode operation? Hardware provides at least 2 modes: user mode and kernel (aka: supervisor, protected) mode.
How does the OS run a program? It does an exec from kernel mode (system mode 1). We go to system mode 0, user mode. When the program exits, we switch back to kernel mode.
How does a user program interact with the kernel? It makes a system call, and the kernel takes over, completes the action, and the kernel returns to the process (back to user mode).
What happens when the hardware needs something to happen? It does an interrupt, and the kernel takes control, switches the process, and once hardware task is complete, does a return from interrupt back to user mode.
What happens when a program makes an exception? The program terminates, and switches to kernel mode.
What is base and bound? It's a memory addressing restriction where a processes are only allowed access to the memory between a base address and the bound + base addresses. Each process has its own base and bound. A drawback is you don't get address 0. Address translation fixes this.
How does the OS know how to handle an interrupt? It keeps an interrupt vector in the memory of the OS. Each interrupt type is mapped to an address to execute. They are just pointers to code in the OS.
What is the difference between PC and uPC? PC is the program counter for the current process. uPC is the PC of the user process we will return to once an interrupt or other OS process switches context.
How are base and bound enforced? They are stored in registers. Access is restricted by the hardware.
How does x86 do segments and stacks? Each process' individual segments (heap, stack, code, static data) get their own base and bound. They don't even need to be adjacent in memory and their location is hidden by memory addressing.
When a process forks, what happens? "The process is paused, and a complete copy is made: code, stack, heap, data, program counter and registers.
The child and parent resume with returning from fork syscall."
What does fork() return? It returns the child process id to the parent, and 0 to the child. < 0 if error.
What does wait() do? Causes the parent process to pause until the child terminates.
What does exec() do? It's a system call to change the currently running program to something else.
What comes back from wait()? on success, returns the process ID of the terminated child; on error, -1 is returned.
What is a signal? A system call to send a notification to another process.
In a child process, what can you do with fork and then exec? Create a completely new process and then exit.
What is a shell? A job control program. It allows a programmer to create and manage a set of programs to do some task.
What is a microsecond? Also known as μs 1 millionth of a second.
What is a nanosecond? 1 billionth of a second. ns
What is a picosecond? A trillionth of a second.
How does the kernel handle reads and writes? It buffers reads so they can be handled as a stream in your program. Writes are buffered and are not written until the kernel flushes the buffer.
What's the difference between the fread, read, fwrite, write I/O calls? The ones with f are high-level I/O and streamed and buffered by the kernel. The non-f are low-level I/O.
When a system call is made, where are parameters stored? In registers.
What is a socket? It's an abstraction of a network I/O queue. It's a method of communication where a producer writes to one side, and a consumer reads from the other side. It's similar to writing and reading a file, but no file is involved.
What sockets are in modern use? Local sockets to local machine, called UNIX sockets, and TCP/IP and UDP/IP.
What is the GIL? It's the Global Interpreter Lock. It's is a part of CPython. It ensures only one thread runs in the interpreter at once. Having the GIL simplifies many low-level details (memory management, callouts to C extensions, etc.)
When is the GIL released? During I/O (disk IO, network IO, output to display) including when a thread uses sleep.
"What is a ""tick"" in CPython?" Approximately 1 machine instruction.
"What happens every 100 ""ticks"" in the CPython interpreter?" A thread check occurs during which the thread releases the GIL then attempts to reacquire it. Other Python threads will contend for the the GIL. This is no longer the case in 3.4.
What is a lock in CPython? It's a binary semaphore. It's not a mutex lock.
What happens when the heap gets too large? It does a page fault, and the kernel will allocate more memory.
What happens when the heap and stack meet in memory? A guard page is hit and the process is killed.
Where is information about a process stored? In a PCB (process control block).
Where is information about a thread stored? In a TCB (thread control block).
What do multiple threads in the same process share? Heap, file descriptors, code, static data.
What do threads in a process NOT share? Registers and stack.
What can happen with thread stacks if one goes into a deep recursion? One thread's stack can grow into another thread's stack and write over it. A guard page can help to protect from that.
What can cause a thread to give control back to the dispatcher? Thread returns control voluntarily (yield, requesting I/O (which blocks), wait for signal from another thread) or gets preempted by an interrupt.
How long does it take to do a process context switch? 3-4 microseconds.
How long does it take to perform a thread context switch? 100ns
How often do context switches happen? Every 10-100 ms.
Context switch time increases sharply with the size of what? (by 100x or more.) The working set - the subset of memory used by the process in a time window. Cache etc.
What happens in a system call to get the OS to switch to kernel mode? A trap.
How many threads should you run per process? One per core.
How is concurrency accomplished? By multiplexing CPU time.
What's the difference between parallelism and concurrency? Concurrency means running multiple blocks of instructions independently. Parallelism means running instructions at the same time, as on multiple cores at once.
What is oversubscription? Spawning more threads than available cores.
What is a race condition? When the outcome of a deterministic procedure becomes non-deterministic based on differences in subprocess timing.
What can you put in place to exclusively use a resource without another process interfering? A mutex, or even better, a lock guard.
How do you use a mutex in Python? "import threading
lock = threading.Lock()
[first process]
global lock
lock.release()
...
[other process]
global lock
lock.acquire() // attempts to get access, waits if it can't"
What does a future do? Allows us to receive a return value from a function in a child thread.
What is a promise? A promise to send a parameter to a child thread's function later.
What is livelock? "It occurs when multiple processes are attempting to deal with the current state, but neither makes progress. This can happen when a system is attempting to resolve a deadlock situation but another or the same process continue to trigger it.
Starvation is another example."
How long does a terminated process stay in the terminated state? Until the parent process does a wait to receive its exit code.
In Python, what can you use to fork a process? The multiprocessing module. It supports process Pool and Process for making a pool of worker processes or forking temporary subprocesses.
What does the concurrent.futures module offer? "ThreadPoolExecutor
ProcessPoolExecutor
Executor objects
Future objects"
What is an interrupt? A hardware-invoked context switch. The interrupt handler always runs immediately.
What happens during an interrupt? The currently running process' state is saved. We switch to kernel mode, the interrupt handler runs, and once its complete, the system goes back to user mode and the process continues.
What really happens when you fork a process? A fork doesn't copy everything, it just duplicates the page table pointers, which are all set at read-only. Called copy-on-write. Once you write to memory, then it copies the state.
What is multiprocessing? Parallel execution on multiple cores.
What does a PCB contain? "Everything about a process:
- status
- register state (when not in ready state)
- PID, User, Executable, Priority
- Execution time
- Memory space, translation"
What is special about an interrupt handler? It disables interrupts and runs to completion.
What are the five states a process can be in? "- new (when being created)
- ready
- running
- waiting (for I/O or event coordination)
- terminated (waits for parent process to receive its exit code)"
What is the difference between filter() and map()? "Filter uses a function that returns true or false (predicate).
Map uses a function that returns a value."
What is synchronization? Using atomic operations to ensure cooperation between threads.
What is a critical section? A block of code that multiple threads within a process could try to access at the same time. To ensure correct processing, the critical section should be locked before entering, then unlocked when leaving. This creates a mutual exclusion on shared data.
What is the priority inversion problem? A thread that is busy-waiting for a lock to be released ends up stealing CPU and getting a higher priority than the thread with the lock. SO since the waiting thread gets higher priority, the thread holding the lock can't complete and release the lock.
What is busy-waiting? One or more threads is using a lot of CPU by continuously checking a value, or test&set() checking and writing a value in wiating for a lock to release, thus stealing CPU from the thread holding the lock.
What is a semaphore? "A semaphore limits access to a maximum number of threads (maximum concurrency) to have access to a source at the same time. It is commonly used to limit database connections.
A semaphore (defined by Dijkstra) is kind of signaling solution for handling concurrency data integrity problems that arise in multi-threaded applications. It has a non-negative integer that supports 2 operations:
- P() [proberen, to test/probe] - atomic operation that waits for semaphore to become > 1, then decrements it by 1 (wait)
- V() [verhogen, to increment] - an atomic operation that increments the semaphore by 1, waking up any P (signal)
The initial semaphore value will determine how many threads can run in the critical section at once."
What's another name for a mutual exclusion? Binary semaphore.
What is a monitor? A lock and zero or more condition variables for managing concurrent access to shared data.
What should locks and condition variables each be used for? "locks - mutual exclusion
condition variables - scheduling constraints"
What is a condition variable? A queue of threads waiting for access to something in a critical section.
What is a special feature of condition variables? It allows sleeping inside a critical section by atomically releasing lock at the time we sleep.
What are the 3 methods on a condition variable? "wait(&lock)
signal() - signals the next waiting member
broadcast() - signals all waiting members"
What type of scheduling do most modern processors use? Mesa-scheduling.
What are some things the scheduler tries to accomplish? "- minimize response time
- maximize throughput
- fairness"
What is a drawback of context switching? CPU cache misses as thread comes back from switching and finds the CPU cache doesn't have the values it had before.
What's the convoy effect? Short processes get stuck behind long processes in a FIFO style ready queue.
What is the round robin scheduling scheme? Each process gets a time quantum q milliseconds to run. 10-100ms, the q is tunable. Each process runs for that time slice (or until completion if close to done) and then goes back on the ready queue.
What are pros of the round-robin scheduling scheme? "- better for short jobs (they fit in the time slice)
- fair"
What is a con of the round-robin scheduling scheme? Long jobs take longer because context-switching time adds up.
How long does context switching take? "0.1ms - 1ms
It's roughly 1% overhead."
What is starvation? When low-priority jobs never get run because there are always higher priority jobs running.
How does a process' priority get changed? The scheduler utilizes heuristics on interactivity, locking, burst behavior, etc.
What are some methods of avoiding deadlock? "- don't allow waiting for a resource (means a lot of retries)
- make all threads request everything they'll need at the beginning
- force all threads to request resources in a particular order preventing any cyclic uses of resources (so no cycle exists)
- temporarily expand resources when a deadlock is detected"
What is the banker's algorithm for preventing deadlock? "- allocate resources dynamically
- evaluate each request and grant if some ordering of threads is still deadlock-free afterward
- do so by pretending the request was granted, then running a simulation to see if a deadlock would occur"
How does the banker's algorithm solve the dining lawyers problem? "When you try to grab a chopstick, it's either:
- not the last chopstick
- is last chopstick but someone else will have two afterwards"
Does the CPU use virtual addresses or physical addresses? Virtual addresses
What translates virtual to physical addresses? The MMU - the memory management unit
What are the four conditions needed for a deadlock? "- mutual exclusion
- hold and wait
- no preemption
- circular wait"
How many bits represent an IPv4 address? 32
How many bits represent an IPv6 address? 128
Name some of the protocols used within the TCP/IP application layer. "- http
- https
- ftp
- tftp
- ntp
- irc
- telnet
- smtp
- ssh
- dns
- snmp
- pop3"
What is NTP? Network time protocol
What are some protocols in the TCP/IP transport layer? "- TCP
- UDP"
What are some TCP/IP network layer protocols? "IP - internet protocol
ICMP - internet control message protocol
ARP - address resolution protocol"
What are some TCP/IP network access layer protocols? "- RJ45
- ISDN
- Microwave
- Ethernet
- Wifi
- Fiber optics
- ATM
- RJ48
- Copper cables"
What is a PDU? "Protocol data unit
- generic term used to describe the information at a given layer in the TCP/IP stack"
What is the PDU for OSI layer 7? data, determined by what information is being exchanged: text, encrypted text, compressed data
What are the PDUs for the the OSI transport layer? "for TCP, it's called a segment
for UDP, it's called a datagram"
What is the PDU for the TCP/IP internet layer? packet
What are the 2 PDUs of the OSI Network Access layer? "data link layer: frames
physical layer: bits"
What is the port for DNS? 53
What is the port for telnet? 23
What is the port for ssh? 22
What is the port range for clients? 8000-65535
How many bits are in an ethernet frame? 48 bits, represented as a hexadecimal number.
What does MAC stand for? medium access control, a sublayer in the data link layer.
What is the PDU and the addressing at the data link layer? "PDU: frame
Addressing: physical (MAC) address"
What devices are at the data link layer? Bridges, switches (multi-port bridge). They inspect frames and forward or not.
What devices are at the Internet/Network layer? "Routers
Layer 3 switches: can be a switch or a router"
What is the PDU and the addressing at the Internet/Network layer? "PDU: packet
Addressing IP address"
What is the PDU and the addressing at the Transport layer? "PDU: segment
addressing: ports"
What devices are at the Transport layer? Firewalls
What is a socket in HTTP? The combination of an IP address and a port.
What is involved in the 3 way handshake (TCP)? "SYN=1 - synchronize, gives a Seq number and expects that number + 1 in response
ACK=1 - sent by acknowledging server with incremented number, who also sends a SYN=1 and a Seq
SYN=0 ACK=1 and the Seq (incremented number) back to the server
Now you're talking!"
Does Kerberos use symmetric or asymmetric encryption? Symmetric. It tracks all principals and their keys in its KDC table.
What are the 7 layers of the OSI model? "- application
- presentation
- session
- transport
- network
- data link
- physical"
What are the 4 layers of TCP/IP? "- application (application, presentation, session in OSI)
- transport
- internet (network in OSI)
- network access (data link & physical in OSI)"
How is an SSL certificate generated by the certificate authority (CA)? "The common name and public key for a given domain name, signed by the certificate authority's secret key.
The browser can verify the cert with CA's public key."
What is the secure flag on a cookie? "When set on a cookie, it will only be sent on https requests.
When not set, cookie will be sent on both http and https requests."
When does a Python multi-threaded program terminate? The entire Python program exits when no alive non-daemon threads are left.
In Python, if a thread is set to daemon, what happens when the thread sleeps? If the Python program reaches its end, the thread will be killed even if it's sleeping.
If a thread is a daemon, what happens when you do a join()? The main thread will wait for it.
What does WebRTC stand for? Web Real-Time Communication
Give an example of the thread-per-connection pattern. A web server might spawn a thread per connection, then reuse that thread once the connection ends, or terminate the thread.
Give an example of the thread pool model. A pool of threads can be maintained in order to quickly provide one as a resource for a database connection.
What is contained in a packet? "- source IP
- destination IP
- data - some portion of the final payload"
What is TLS? The successor to SSL. All of SSL's versions have been deprecated due to security issues.
What is the purpose of the transport layer? To allow multiple applications to use one network connection simultaneously.
What is DNS spoofing? A DNS server is compromised and returns incorrect IP addresses for a some domains.
What does TCP stand for? Transmission Control Protocol
What is special about TCP? "It manages the sending and receiving of packet data.
It acknowledges receipt of packets.
If packets are missing, the source will resend the missing packets."
What is HTTP? The protocol for client-server communication.
What does UDP stand for? User Datagram Protocol.
What is the size of a UDP header? 8 bytes
What is the size of a TCP header? 20 bytes
What does ICMP stand for? Internet Control Messaging Protocol
What does ICMP allow you to do? Allows devices to communicate and send errors. Can echo to see if a device is on the network.
What does SNMP stand for? Simple Network Management Protocol.
What does SNMP do? Gathers info from machines on the network when each box has an SNMP agent installed. Can send a large amount of info about machines, software installed, and machine configuration.
Do you need to establish a connection before sending data via UDP? No, it's connectionless.
Tell me about the checksum in a UDP packet. It's a 16-bit checksum. It's only mandatory on IPv6
How many times are packets sent in UDP? Once.
What is special about UDP? It's connectionless, packets are only sent once and not re-sent if dropped. Packets may not arrive in the right order, and there is no ordering mechanism to fix on the receiving end. No congestion control.
What's special about TCP? "It does a 3-way handshake before data is sent.
Delivery is acknowledged by receiver.
Packets missing within a certain time window are re-requested.
Packets are put in order on receipt.
Congestion control: can delay delivery until network is uncongested.
IPv4 and IPv6: error detection, checksum mandatory."
What does OSI stand for? Open Systems Interconnect
Why was OSI created? To solve the interoperability problem of having multiple heterogeneous networks.
Is OSI just a model? Yes.
What network protocol won the networking wars? TCP/IP, based on the OSI model.
What happens at the Application level of the OSI model? This is where applications live and they handle data in many forms.
What happens in the Session layer of the OSI model? "This layer handles configuration of the data:
- encryption
- compression
- translation to and from different character encodings"
What happens at the Session layer of the OSI model? "This layer controls the communication's access via:
- login rights
- permissions
- rights
- roles"
What happens at the Transport layer of the OSI model? "This layer guarantees end-to-end delivery of data:
- packet ordering
- error detection
- acknowledgements"
What happens at the Network layer of the OSI model? "This layer's function is to find the shortest path through the network to the destination network.
Deals with congestion, bandwidth, etc."
What happens at the Data Link layer of the OSI model? "It decides whose turn it is to talk on the network using bus arbitration methods.
It finds the physical device on the network."
What happens at the Physical layer of the OSI model? "It's the physical network that deals with the physical transmission of electricity through wire:
- cables
- voltages
- frequencies
- connectors
- bits
- transfer rates
- and much more"
How does HTTP/2 save bandwidth? "Headers are compressed and do not need to send the same headers in a session if they haven't changed.
Servers can send assets referenced in a document without waiting for discrete requests for them."
How does HTTP/2 improve cache breaking? A server can send updated assets using server push when it recognizes a file has changed.
What is the stream parallelism in HTTP/2? It's fully multiplexed, so it can use 100-1000 streams in a connection.
Is HTTP/2 binary or textual? HTTP/2 is a binary protocol.
How are headers and body treated differently in HTTP/2? They are split into a header frame and a data frame. Multiple requests can be interleaved in a connection, so a request doesn't block.
What is priority in HTTP/2? Different assets can have different priority so that below the fold content can arrive later.
What is the range of the first octet on a Class A network? 1-126. We don't use 0 or 127.
How many network IDs are there on a Class A network? "2^7 = 128
First bit is 0, bits 1-7 are network IDs"
How many host IDs are supported on a Class A network? "2^24 = 16 million
There are 8 bits for the network ID, and the remaining 24 bits are for host IDs.
So there are 16 million per network."
What is the range of the first octet on a Class B network? 128 - 191
How many network IDs are supported on a Class B network? "2^14 = 16,384
First 2 bits are 10, bits 3-16 are network IDs"
How many host IDs are supported on a Class B network? "2^16 = 65,536
So there are 65,536 per network"
What is the range of the first octet on a Class C network? 192-223
How many network IDs are supported on a Class C network? "2^21 = 2 million
First 3 bits are 110, bits 4-24 are network IDs"
How many host IDs are supported on a Class C network? "2^8 = 256
There are 256 hosts per network"
What is a class D network reserved for? Multicasting
What is unicasting? Sending a packet from one host to another.
What does a network ID end in? 0
What does a broadcast ID end in? 255
Who does a broadcast address of 255.255.255.255 send to? All hosts within the network.
What is a directed broadcast? It's a broadcast to all hosts within another network.
What is a limited broadcast address? The limited broadcast address is the address formed by setting all 32 bits of the IP address to 1 (255.255.255.255). The limited broadcast address is used when an IP node must perform a one-to-everyone delivery on the local network but the network ID is unknown.
Why should you make networks as small as possible? "For:
- security
- maintenance
- management"
How you divide a network? By subnetting.
What does a /27 CIDR mean? The first 27 bits are masked with 1s. The remaining 5 bits are reachable in the subnet.
What does a /24 CIDR mean? The first 24 bits of the IP address are masked. Only hosts with addresses in the unmasked portion are reachable.
What is a block cipher? A block cipher is a method of encrypting text (to produce ciphertext) in which a cryptographic key and algorithm are applied to a block of data (for example, 64 contiguous bits) at once as a group rather than to one bit at a time.
What is QUIC? QUIC is a new transport which reduces latency compared to that of TCP. On the surface, QUIC is very similar to TCP+TLS+HTTP/2 implemented on UDP.
What is Capsicum? A sandboxing framework that adds capability-based security to unix-like kernels and denies access to global namespaces.
What is a global namespace in unixy terms? "aspects of a system that can be accessed from anywhere:
- file paths
- networks
- PIDs"
What is Google Native Client? Also called NaCl, Native Client is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system.
What are web sockets? Full-duplex communication between client and server.
What is the same-origin policy? "Goal: Two websites should not be able to tamper with each other.
Strategy: each resource is assigned an origin. JS can only access resources from its own origin.
Origin: scheme + hostname + port"
How can 2 origins (let's say 2 frames) communicate? window.postMessage (HTML5) allows for sending data messages between two windows/frames across domains.
What is JIT compilation? JIT compilation, also known as dynamic translation, is compilation done at run-time rather than ahead of time (AOT).
What is PyPy? "PyPy is a replacement for CPython. It is built using the RPython language that was co-developed with it. RPython is a subset of Python and can be translated to C. The main reason to use it instead of CPython is speed: it runs generally faster due to JIT compilation.
PyPy implements Python 2.7.10. It supports all of the core language, passing the Python test suite (with minor modifications that were already accepted in the main python in newer versions). It supports most of the commonly used Python standard library modules."
What does SMT stand for? Satisfiability modulo theories.
What is an SMT solver? A satisfiability modulo theories solver solves for large interconnected logic formulas to determine if a given formula can be satisfied. These are helpful for determining the outcome or inputs for a program using symbolic execution.
How do we find that a condition exists that will cause a program to have a predicted outcome? Using an SAT solver (using logic tests) and an SMT solver (that plugs in numbers), and tells the SAT solver where it got it wrong. They provide feedback in a cycle to each other until a condition can be found.
What is transactional memory? Transactional memory attempts to simplify concurrent programming by allowing a group of load and store instructions to execute in an atomic way. It is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing.
What is software transactional memory? Software transactional memory provides transactional memory semantics in a software runtime library or the programming language, and requires minimal hardware support (typically an atomic compare and swap operation, or equivalent). As the downside, software implementations usually come with a performance penalty, when compared to hardware solutions.
Does PyPY have a GIL? Yes. The GIL is very difficult to remove. You can use pypy-stm instead, which uses software transactional memory, but will suffer a performance penalty.
How can a server deal with a SYN flood attack? When it detects a large number of SYN packets at once, or the size of its SN (sequence number) data structure reaches a certain threshold of entries, it can switch to a stateless version, where it send SN responses as signed values with a timestamp, and if it receives one back it lets them through without needing a lookup table.
What is a stack canary? It's a buffer overflow defense where a random value is pushed onto the stack after the saved EBP, and before tearing down the stack frame, the value is checked. Any buffer flow targeting the return instruction pointer would have to have overwritten this value.
What type of buffer overflow protection does gcc and Visual Studio employ? They use a stack check guard of bytes before and after the buffer's allocated memory. Once values are written to the buffer, the bytes are checked to ensure they are still the same.
What is scalability? Scalability is the measure to which a system can adapt to a change in demand for resources, without negatively impacting performance.
What is Akka? "An open source project that provides a simpler, single programming model - one way of coding for concurrent and distributed applications - the actor programming model.
Akka’s primary goal is to make it simpler to build applications that are deployed in the cloud or run on devices with many cores and that efficiently leverage the full capacity of the computing power available. It’s a toolkit that provides an actor model, runtime, and required supporting tools for building scalable applications."
What is an actor? "Briefly, actors are a lot like message queues without the configuration and message broker installation overhead. They’re like programmable message queues shrunk to microsize—you can easily create thousands, even millions of them. They don’t “do”
anything unless they’re sent a message.
Messages are simple data structures that can’t be changed after they’ve been created, or in a single word, they’re immutable.
Actors can receive messages one at a time and execute some behavior whenever a message is received. Unlike queues, they can also send messages (to other actors).
Everything an actor does is executed asynchronously. Simply put, you can send a message to an actor without waiting for a response. Actors aren’t like threads, but messages sent to them are pushed through on a thread at some point in time. How actors are connected to threads is configurable - this is not a hardwired relationship.
For now the most important aspect of actors is that you build applications by sending and receiving messages. A message could be processed locally on some available thread, or remotely on another server. Exactly where the message is processed and where the actor lives are things you can decide later, which is very different compared to hardcoding threads and RPC style networking. Actors make it easy to build your application out of small parts that resemble networked services, only shrunk to microsize in footprint and administrative overhead."
What is an IDL-based encoding? "An interface description language or interface definition language (IDL) encoding.
It requires a schema definitions. They offer peace of mind with respect to data format and validation for consumers while sacrificing flexibility in the schema’s evolution."
What is Tarantool? An in-memory noSQL database that uses write-ahead logging for crash resistance and persistence.
What is a coroutine? "An object representing activity that eventually completes. Also refers the the function we call that returns a coroutine.
In Python, coroutines are generators."
What is a future? An object representing a result that may not be available yet.
What is AQP? Approximate query processing. It means pulling a sample of data instead of taking time to process an exact result. It is often used when a data storage involves terabytes or more.
How would you visualize billions of items in a graph? In many cases, you don't need to graph every point, just use visualization-aware sampling. Sometime 1% or less will do.
What is F1/Spanner? Fault-Tolerant Distributed RDBMS (Spanner) Supporting Google's Ad Business (F1)
What is Photon? Fault-tolerant and Scalable Joining of Continuous Data Streams
What is Mesa? Geo-Replicated, Near Real-Time, Scalable Data Warehousing
How does Google use multi-homed datacenters? A multi-homed system runs live in multiple datacenters all the time. Each datacenter processes work all the time, and work is dynamically shared between datacenters to balance load. When one datacenter is slow, some fraction of work automatically moves to faster datacenters. When a datacenter is completely unavailable, all its work is automatically distributed to other datacenters.
What is ElasticSearch? Open Source, Distributed, RESTful Search Engine
What is an example of a circuit breaker? Start sending 503s if your service is choked to avoid numerous simultaneous retries that just make the system worse.
What is the name of Google's search ranking algorithm? Hummingbird. PageRank is just one factor used by the algorithm.
What is celery? Distributed Task Queue
LRU is the most popular type of what kind of policy? Eviction
What does an eviction policy try to predict? An eviction policy tries to predict which entries are most likely to be used again in the near future, thereby maximizing the hit ratio
What is Caffiene? Caffeine is a high performance, near optimal caching library based on Java 8.
What is request coalescing? When many requests arrive for some content that’s missing in the cache (cache miss), only one instance request will proceed to the backend to fetch the content on behalf of all to avoid a flood.
When might you need to use a NoSQL database "You don’t have any relational data.
If you need to store > 5 TB of data or you have an incredibly data intensive workload.
Your application has super low-latency requirements.
You need really high throughput."
What is AMP? "AMP is a restricted subset of HTML designed to make the web fast on mobile devices.
Provides a shared library of scripts so they don’t have to downloaded every time.
Sets standards for the architecture of advertising so advertising doesn't infect pages.
Shared mechanism for collecting data for analytics so you don’t have N different packages on a page gathering stats."
What is a benefit of making processes asynchronous? Flexibility in the architecture. Getting users on hooked on synchronous low-latency interactions doesn't allow for architecture flexibility.
What is BASE? "basically available
soft state
eventually consistent
A BASE based system is more tolerant to latency because it is an inherently partitioned and loosely coupled architecture and it uses eventual consistency."
What is ACID? "atomicity
consistency
isolation
durability"
What is the CAP theorem? "The CAP theorem, also named Brewer's theorem, states that it is impossible for a distributed computer system to simultaneously provide all three of the following guarantees:
- Consistency (all nodes see the same data at the same time)
- Availability (every request receives a response about whether it succeeded or failed)
- Partition tolerance (the system continues to operate despite arbitrary partitioning due to network failures)"
What is zero copy? Applications that use zero copy request that the kernel copy the data directly from the disk file to the socket, without going through the application. Zero copy greatly improves application performance and reduces the number of context switches between kernel and user mode.
What is a metaclass? Any callable (function or class) that implements type()'s function signature.
What is privilege separation? Separating an application into different areas so a vulnerability in one area doesn't affect the entire application.
In security, what is a principal? An entity with privileges or rights.
In Unix, who is the owner of a file? The user with the user ID that matches the UID of the inode.
What privilege do you need to lookup files or directories in a path? The executable permission.
When is security enforced on a file? Security is checked when the file descriptor is created. Then it's up to the user to be careful and secure the file descriptor.
What is ptrace? ptrace is a system call found in several Unix and Unix-like operating systems. By using ptrace, one process can control another, enabling the controller to inspect and manipulate the internal state of its target.
What can you use to debug a process? ptrace
What user privilege is required to bind to ports < 1024? root
During system bootstrapping, what call is performed by the system to give a non-root user the ownership of a process? setuid()
What are the setuid binaries? "su
sudo"
What does chroot do? Changes the root directory (/) for a user to be a directory on the filesystem where they can't escape.
What is a confused deputy? "A confused deputy is a computer program that is innocently fooled by some other party into misusing its authority. It is a specific type of privilege escalation. In information security, the confused deputy problem is often cited as an example of why capability-based security is important, as capability systems protect against this whereas access control list-based systems do not.
The classic example is a Fortran compiler that creates a billing record for each use. A user was able to tell the compiler to output a binary with the same name as the billing file, overwriting it."
What is an example of a confused deputy in the web frontend world? A CSRF attack.
What is ambient authority, or ambient privilege? The decision about whether a process or agent can perform an action is based on information not explicitly stated, but inherited instead.
What is a capability? The privilege to act upon something given your ownership of it, and the inability to act on something using an intermediate process' privileges. An example would be a function where you pass a file descriptor as an argument and the function uses your capability, not its own.
What is a requirement of enabling sandboxing? "The kernel must be able to support it by disallowing system calls that reference global namespaces:
- file paths starting at root - must be relative
- network
- PIDs - use process descriptors instead"
How is RSA decryption optimized for speed? "- c^d mod p and c^d mod q are processed in parallel and merged at the end using the Chinese remainder theorem
- put into Montgomery format
- sliding windows to exponentiate on bits of exponent
- perhaps a single extra reduction
- convert back from Montgomery format
- merge using CRT"
How do you change a positive integer to negative? Subtract 1, flip all bits
How do you change a negative integer to positive? Flip all bits, then add 1
What is Intel architecture? "CISC - Complex Instruction Set Computer
Many special-purpose instructions."
What size are Intel instructions? They are variable-length, from 1 to theoretically 16 bytes.
What is RISC? "Reduced Instruction Set Architecture.
- more registers
- fewer, fixed-sized instructions
- used in PowerPC, ARM, SPARC, MIPS"
What Endianness is Intel? Little Endian, but only in memory. In registers, all are Big Endian.
What is Little Endianness? The least significant bytes of a word or larger are stored in the lowest address. All bytes are the same. There is no Endianness within a byte.
How many registers are on CISC? 8 general-purpose registers and an instruction pointer. 2 of 8 are not that general.
What is the word size on Intel? 16 bits
What is the double-word size on Intel? 32 bits
What is Big Endianness? The most significant bytes of a word or larger are stored in the lowest address.
What is the EAX register used for? Stores the function's return value.
What is the EBX register used for? Base pointer to the data section.
What is the ECX register used for? Counter for string and loop operations.
What is the EDX register used for? I/O pointer
What is the ESI register used for? Source pointer for string or other copy operations.
What is the EDI register used for? Destination pointer for string or other copy operations.
What is the ESP register used for? Stack pointer
What is the EBP register used for? Stack frame base pointer
What is the EIP register used for? Instruction pointer - next instruction pointer to execute.
What are the caller-save registers? "eax
edx
ecx"
What are the callee-save registers? "ebp
ebx
esi
edi"
Other than the main registers, what other registers can be used? Legacy 8 and 16-bit registers: AX, AH, AL, SP, BP, SI
What is the caller register-saving convention on Linux systems? cdecl - C declaration
What is avalancing? The effect of a hashing method where a small change in the input has a large effect on the output.
What is Chef? A configuration tool. You write or reuse recipes that declare the state you wish your server to be in. It calculates the delta and builds out for you.
What is an example of a non-cryptographic hash function? MurmurHash is an efficient, non-cryptographic hash function suitable for general hash-based lookup. The name comes from two basic operations, multiply (MU) and rotate (R), used in its inner loop. It has an avalanche effect. The current version is MurmurHash3 which yields a 32-bit or 128-bit hash value.
How could you process calculations on elements of an array in parallel? Using recursion, divide and conquer, breaking down the array into smaller segments, then merging the values as the recursion unwinds. Non-mutation of the array means locking is not required.
What factor should you keep in mind when doing parallel computation on different parts of a large data structure? The memory bandwidth of your RAM. It can become a bottleneck.
What will be the computation time when processing multiple tasks? The length of time the longest subcomputation takes.
Why should you avoid starting and joining a task or thread on the same line or proximity? Execution on the current thread will block until it completes, thereby obviating the concurrent call.
What factors affect performance? "- processor speed
- number of processors
- memory access latency and throughput
- cache behavior
- runtime behavior (garbage collection, JIT compilation, thread scheduling)"
What is Amdahl's Law? It gives the theoretical speedup in latency of the execution of a task at fixed workload that can be expected of a system whose resources are improved.
What aspects are important in measuring and benchmarking performance, parallel or otherwise? "- multiple repetitions
- statistical treatment - mean and variance
- eliminate statistical outliers
- ensuring steady-state (warm-up)
- preventing anomalies (garbage collection, JIT compilation, compiler optimizations in benchmark code that misrepresents real-world)"
When associative operations are used, what does it mean? Grouping doesn't matter. It will evaluate to the same result.
What is a synonym of reduce()? fold()
What is the commutative property? The condition that a group of quantities connected by operators gives the same result whatever the order of the quantities involved. So order of operands doesn't matter, but grouping may matter.
What is a bag? A multiset.
What is a multiset? A set in which elements do not have to be unique.
What are some examples of operations that are both associative and commutative? "- Addition and multiplication of integers
- Union, intersection, and symmetric difference of sets
- Addition of vectors
- Addition of matrices of fixed dimension
- Addition and multiplication of polynomials"
What are some examples of operations that are associative but not commutative? "- concatenation of lists
- concatenation of strings
- matrix multiplication"
What should we worry about floating point addition and multiplication? It is commutative but not associative.
What is the rule of thumb before optimizing or parallelizing? Ensure the code works correctly first. Then, if you want to get fancy, you can begin to optimize your code for greater speed.
Does Python have an opcode cache? In a way. It outputs a .pyc file, containing the bytecode. When a module is imported for the first time, or when the source is more recent than the current compiled file, a .pyc file containing the compiled code will usually be created in the same directory as the .py file. When you run the program next time, Python uses this file to skip the compilation step.
Design a URL shortening service. ask:
How would you design the feature in LinkedIn where it computes how many hops there are between you and another person? ask:
If you were to design a web platform for online chess games, how would you do that? "ask:
-"
What are some key things to remember when scaling a large system? "1) Asynchronous is good (use queues, topics/pub-sub)
2) Parallel is good (multi-threading, load balancing etc.)
3) Avoid points of contention e.g. synchronization
4) Avoid writing to disk until you must - cache like crazy
5) Scale out not up
6) At web scale the speed of light is a problem
7) At web scale everything fails - networks, load balancers etc."
What is Thrift? "Apache Thrift is a framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between different languages. It handles serialization and has its own communication protocol.
IDL-based."
What is Memcache? An in-memory distributed hash table. It supports only a few commands but it is extremely efficient.
How does/did Facebook use memcache and mySQL in 2009? No joins in production. They have many logical databases for all of their types: people, events, place info, etc. They treat the web tier as a CPU, memcache as system memory, and the database as disk. Everything has an ID and you use the ID to query memcache using a multiget. Any misses are fetched from the database and cached in memcache.
What is an out-of-band cache? A cache layer that does not synch with persistent storage. When changes are made to the database, there are no notifications to synchronize with the cache. The cache entry would need to be updated or evicted by other means.
What is a monad? In functional programming, monads are a way to build computer programs by joining simple components in predictable and robust ways. A monad is a structure that represents computations defined as sequences of steps: a type with a monad structure defines what it means to chain operations together, or nest functions of that type. This allows the programmer to build pipelines that process data in a series of steps (i.e. a series of actions applied to the data), in which each action is decorated with additional processing rules provided by the monad. A monad is defined by a return operator that creates values, and a bind operator used to link the actions in the pipeline.
What is pyramidpypi? A self-hosted mirror of pypi.
Big Omega represents what? A lower bound on the growth of a function. f grows at least as fast as g.
Theta represents what? A tight asymptotic bound on a function, in other words if both f and g have approximately the same rate of growth.
For graph problems, the complexity Theta(N + M) is known as what? linear in the graph size
"What is a better way of saying ""at least O(n^2)""?" big Omega(n^2)
What is data normalization? "Normalization is a systematic approach of decomposing tables to eliminate data redundancy and undesirable characteristics like insertion, update and deletion anomalies.
Normalization is used for mainly two purpose,
- eliminating redundant (useless) data
- ensuring data dependencies make sense"
What are the requirements for first normal form? "- each cell has a single value
- all items in a column must be of the same type
- rows are uniquely identified by a unique ID or a composite key"
What are the requirements for second normal form? All attributes (non-key columns) are dependent on the key
What are the requirements for third normal form? All fields can only be determined by the key in the table and no other column.
What are the requirements for fourth normal form? No multi-valued dependencies, meaning records should not be duplicated in a table just because more than one item is associated. This creates records that are duplicates except for one field.
What are some use cases of Hadoop? "- reporting on user behavior over many events
- log processing of 100s of billions of rows"
What are some solid principles to keep in mind for scaling? "- Keep it very simple
- Don’t re-invent the wheel
- Go with boring, proven and well-supported technologies when you can
- Build for what you will need over the next 12-18 months
- Make different things look the same
- Cache to protect the database
- Good enough is good enough"
What is gunicorn? A Python WSGI HTTP Server
What is WSGI? "WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request. It was outlined in PEP 3333.
A WSGI server (meaning WSGI compliant) only receives the request from the client, pass it to the application and then send the response returned by the application to the client."
What is Fabric? Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
What is Apache Solr? "Solr is an open source enterprise search platform built on Apache Lucene.
Solr is highly reliable, scalable and fault tolerant, providing distributed indexing, replication and load-balanced querying, automated failover and recovery, centralized configuration and more."
What is munin? Graph metrics and alerting.
What tends to become an issue as you add services in a datacenter and have to ping more caches and services to fulfill a request? Fan-in, where you can overload routers due to increased internal network traffic.
What can Redis be used for? "- a noSQL key-value store
- caches
- queues"
What is a benefit of sharding a database as it grows, and what determines the size? Keeping the index in cache ensures a user lookup doesn't have to hit the disk, lookups can be served from RAM. How much RAM you have will determine the index size which will hint at the underlying data size.
Why do most businesses end up sharding as they scale? To support massive concurrent writes.
What is a message broker? Message broker is an intermediary program module that translates a message from the formal messaging protocol of the sender to the formal messaging protocol of the receiver. Message brokers are elements in telecommunication networks where software applications communicate by exchanging formally-defined messages. Message brokers are a building block of Message oriented middleware.
What are some examples of message brokers? "Apache ActiveMQ
Apache Kafka
Apache Qpid
Celery
Gearman
HornetQ (Red Hat)
IBM Integration Bus
JBoss Messaging (JBoss)
JORAM
Microsoft BizTalk Server (Microsoft)
Microsoft Azure Service Bus (Microsoft)
NATS (MIT Open Source License, written in Go)
Open Message Queue
Oracle Message Broker (Oracle Corporation)
QDB (Apache License 2.0, supports message replay by timestamp)
RabbitMQ (Mozilla Public License, written in Erlang)
SAP PI (SAP AG)
Spread Toolkit
Tarantool, a NoSQL database, with a set of stored procedures for message queues
WSO2 Message Broker
Enduro/X Transactional Message Queue (TMQ)"
What is Ehcache? Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It's the most widely-used Java-based cache.
Timing of a L1 cache reference? 0.5 nanoseconds (ns)
Timing of a branch misprediction? 5 nanoseconds (ns)
Timing of a L2 cache reference? 7 nanoseconds (ns)
Timing of a mutex lock/unlock? 25 nanoseconds (ns)
Timing of a main memory reference? 100 nanoseconds (ns)
Timing to compress 1KB? 3,000 nanoseconds (3 microseconds)
Time to send 2K bytes over 1 Gbps network? 10,000 nanoseconds (0.01 ms)
Time to read 4KB randomly from an SSD? 150,000 nanoseconds (0.15 ms)
Timing to read 1MB sequentially from memory? 250,000 nanoseconds (0.25 ms)
Timing of a disk seek? 10,000,000 nanoseconds (10 ms)
Time to read 1MB sequentially from an SSD? "1,000,000 nanoseconds (1 ms)
4x memory"
Timing to read 1MB sequentially from disk? "20,000,000 nanoseconds (20 ms)
80x memory
20x SSD"
Timing to send a packet CA -> Netherlands -> CA? 150,000,000 nanoseconds (150 ms)
Timing of a context switch between processes? 3000 nanoseconds
Timing of fork()? Between 70,000 and 160,000 nanoseconds.
What is the timing overhead for a system call? 400 nanoseconds
What is Kafka? "Apache Kafka is pub-sub messaging rethought as a distributed commit log.
Kafka is a distributed, partitioned, replicated commit log service. It provides the functionality of a messaging system, but with a unique design.
A single Kafka broker can handle hundreds of megabytes of reads and writes per second from thousands of clients."
What is GAE? Google App Engine is a platform for building scalable web applications and mobile backends. App Engine provides you with built-in services and APIs such as NoSQL datastores, memcache, and a user authentication API, common to most applications.
What is GDS? "Google Cloud Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. Cloud Datastore features include:
Atomic transactions.
Massive scalability with high performance.
Flexible storage and querying of data.
Balance of strong and eventual consistency.
Encryption at rest.
Fully managed with no planned downtime."
What is the problem that serialization introduces? The overhead of serializing and deserializing. It's all expensive, and for Python, it can be terribly slow.
What does the Python bisect module do? The bisect module, part of the standard library, provides support for maintaining a list in sorted order without having to sort the list after each insertion. For long lists of items with expensive comparison operations, this can be an improvement over the more common approach.
What is PycURL? "PycURL is a Python interface to libcurl. PycURL can be used to fetch objects identified by a URL from a Python program, similar to the urllib Python module. PycURL is mature, very fast, and supports a lot of features.
PycURL is targeted at an advanced developer - if you need dozens of concurrent, fast and reliable connections or any of the sophisticated features listed above then PycURL is for you.
The main drawback of PycURL is that it is a relatively thin layer over libcurl without any of those nice Pythonic class hierarchies. This means it has a somewhat steep learning curve unless you are already familiar with libcurl's C API."
How does PycURL compare to requests? "PycURL can handle a large number of multiple concurrent requests. When reusing connections, it can perform more than 2,000 requests per second.
pycurl takes about 73 CPU-microseconds to issue a request when reusing a connection
requests takes about 526 CPU-microseconds to issue a request when reusing a connection
pycurl takes about 165 CPU-microseconds to open a new connection and issue a request (no connection reuse), or ~92 microseconds to open
requests takes about 1078 CPU-microseconds to open a new connection and issue a request (no connection reuse), or ~552 microseconds to open"
What is ZooKeeper? "Apache ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them, which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.