Compsci 201 – Ultimate Study Guide
1■■ Java Fundamentals & OOP Basics
Primitive types (int, double, boolean, char) hold values directly; objects hold references (String,
ArrayList). Use 'new' to create objects. Strings are immutable; s = s + "x" creates a new one. '=='
checks identity, '.equals()' checks content. Access modifiers: private fields with public getters/setters.
Constructors initialize objects; 'this' refers to current instance. Example: public class Person { private
String name; public Person(String n){this.name=n;} public String getName(){return name;} }
2■■ Arrays, ArrayLists, and Collections
Array access a[i] O(1); ArrayList add(x) O(1) amortized; add(i,x) O(N); contains O(N); get O(1); remove
O(N). Collections.sort(list) O(N log N); Collections.frequency(list,x) O(N). Copy via new ArrayList<>(list)
O(N). ArrayList: fast random access, slow insert front. LinkedList: slow access, fast insert/remove ends.
3■■ Big-O & Loop Patterns
for(i=0;i<N;i++) O(N); for(i=1;i<N;i*=2) O(log N); for(i*i<N;i++) O(√N); nested loops multiply; sequential
add. Triangular for(j<i) O(N²). i*i<i condition runs √N. i*=i yields O(log log N). Outer doubling + inner
linear = O(N log N). Example: for(i=1;i<N;i*=2) for(j=0;j<N;j++){} => O(N log N). for(i=1;i<N;i*=2)
for(j=0;j<i;j++){} => O(N).
4■■ Hashing, HashMap & HashSet
hashCode() -> int fingerprint. equals() compares content. Contract: equals⇒same hashCode, but same
hashCode■equals. HashMap put/get O(1) avg, O(N) worst. Collisions: same bucket; resolved with
equals(). HashSet uses HashMap internally. Example reverse map (state→colleges):
for(Map.Entry<String,String> e : collegeToState.entrySet()){String s=e.getValue();
stateToColleges.computeIfAbsent(s,k->new ArrayList<>()).add(e.getKey());}
5■■ String Methods & Runtimes
s.equals(t) O(L); s.hashCode() O(L) first, O(1) cached; s.toCharArray() O(L); s.substring(a,b) O(L’);
Arrays.asList(arr) O(1); Collections.max(list) O(N). L = string length. String ops scale linearly with
length. Example palindrome check: for(i=0,j=s.length()-1;i<j;i++,j--){if(s.charAt(i)!=s.charAt(j))return
false;}
6■■ Common Library Runtimes
new ArrayList<>(list) O(N); Arrays.asList(arr) O(1); Collections.sort(list) O(N log N);
Collections.frequency(list,x) O(N); list.add(x) O(1)*; list.add(i,x) O(N); list.get(i) O(1); list.contains(x)
O(N); map.put(k,v), get(k) O(1) avg. Example: for(String w:list){Collections.frequency(list,w);} ⇒ O(N²).
7■■ LinkedList vs ArrayList
ArrayList add end O(1)*, add front O(N), get(index) O(1). LinkedList add front O(1), get(index) O(N).
Use LL when inserting/removing ends often.
8■■ Algorithm Examples
Count unique (ArrayList.contains) O(N²); with HashSet O(N). Divisors up to N → O(N); up to √N →
O(√N). Example optimized divisors:
for(k=1;k*k<=N;k++){if(N%k==0){res.add(k);if(k!=N/k)res.add(N/k);}}
9■■ Exam Traps & Facts
Arrays.asList(arr) wraps (O(1)), not copy. Collections.frequency O(N). map.get missing key → null; use
getOrDefault. map.putIfAbsent constructs arg before checking; computeIfAbsent lazy. HashMap
operations average O(1). String literals '==' true via interning; new String() false.
■ Summary Tables & Equations
Loop: i++ → O(N); i*=2 → O(log N); i*i<N → O(√N); nested→multiply; sequential→max term. Sorting
O(N log N). ∑ i=1..N i = O(N²); ∑ 2^k=O(N). String ops scale O(L). Big-O: ignore constants/low terms.
HashMap average constant-time lookup.