Software Engineering
Math
Prime Numbers
Permutations/Combinations
Bit Manipulation
^ XOR, ~ boolean NOT
0110 + 0110 is the same as 0110 * 2, shifting 0110 left by 1.
Multiplying by 2^2 just shifts a number by n
a ^ (~a) is all 1's
bit shifting
arithmetic shift
logical shift
circular rotate
Uses
boolean isPalindrome(int x)
get bit - num & (1 « i) != 0
set bit - num | (1 « i)
clear bit - num & mask, mask = ~(1 « i)
clear from MSB or 0
-
In Java, all integer types are signed, and the “«” and “»” operators perform arithmetic shifts.
Java adds the operator “»>” to perform logical right shifts,
but because the logical and arithmetic left-shift operations are identical, there is no “«<” operator in Java.
Basic Data Structures
Strings
Arrays
Linked Lists
Node deleteNode(Node head, int d) {
Node n = head
if (n.data == d) {
return head.next; // moved head
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head; // head didn't change
}
n = n.next;
}
return head;
}
Advanced Data Structures
Stacks
Queues
Trees
balanced or unbalanced, full and complete
Tries - Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string.
-
HashMap
public HashMap<Integer, Student> buildMap(Student[] students) {
HashMap<Integer, Student> map = name HashMap<Integer, Student>();
for (Student s : students) map.put(s.getId(),s);
return map;
}
If you're only interested in the keys, you can iterate through the keySet() of the map:
Map<String, Object> map = ...;
for (String key : map.keySet()) {
// ...
}
If you only need the values, use values():
for (Object value : map.values()) {
// ...
}
Finally, if you want both the key and value, use entrySet():
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
One caveat: if you want to remove items mid-iteration, you'll need to do so via an Iterator (see karim79's answer). However, changing item values is OK (see Map.Entry).
Algorithms
Design
4 principles of OOP
Data Abstraction
Encapsulation
Inheritance
Polymorphism
Object-Oriented
System Design
Memory Limits
Testing
who
what cases
bounds/environmental
failure conditions
how to test
manual/automated
black/whitebox
values
normal
extremes
nulls
strange
troubleshooting
-
teaching notes