0% found this document useful (0 votes)
5 views4 pages

Java Interview Cheatsheet

This Java Interview Cheatsheet provides essential information on collections, maps, lists, strings, arrays, queues, stacks, and common patterns relevant for interviews at Stripe and FAANG companies. It includes code snippets for declaration, initialization, common operations, and specific patterns such as frequency count and sliding window. The document serves as a quick reference guide for Java programming concepts and techniques useful in technical interviews.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java Interview Cheatsheet

This Java Interview Cheatsheet provides essential information on collections, maps, lists, strings, arrays, queues, stacks, and common patterns relevant for interviews at Stripe and FAANG companies. It includes code snippets for declaration, initialization, common operations, and specific patterns such as frequency count and sliding window. The document serves as a quick reference guide for Java programming concepts and techniques useful in technical interviews.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Interview Cheatsheet — Stripe / FAANG

1. Collections — Declaration & Init


List Map
List<Integer> list = new ArrayList<>(); Map<String,Integer> map = new HashMap<>();
List<Integer> list = new LinkedList<>(); Map<String,Integer> map = new LinkedHashMap<>(); //
List<Integer> list = [Link](1,2,3); // fixed ordered
List<Integer> list = new Map<String,Integer> map = new TreeMap<>(); // sorted
ArrayList<>([Link](1,2,3));

Set PriorityQueue (Heap)


Set<String> set = new HashSet<>(); PriorityQueue<Integer> pq = new PriorityQueue<>(); //
Set<String> set = new LinkedHashSet<>(); // insertion min
order PriorityQueue<Integer> pq = new
Set<String> set = new TreeSet<>(); // sorted PriorityQueue<>([Link]()); // max
PriorityQueue<int[]> pq = new
PriorityQueue<>((a,b)->a[1]-b[1]);

Queue / Deque / Stack Initialise with values


Queue<Integer> q = new LinkedList<>(); [Link]("a","b","c") // immutable
Deque<Integer> deque = new ArrayDeque<>(); [Link](1,2,3) // immutable
Deque<Integer> stack = new ArrayDeque<>(); // use as [Link]("k1",1, "k2",2) // immutable
stack new HashSet<>([Link]("a","b"));

2. Map — Common Operations


Read / Write Iterate
[Link]("k", 1); for ([Link]<String,Integer> e : [Link]())
[Link]("k"); // null if missing [Link]([Link]()+":"+[Link]());
[Link]("k", 0); // safe get for (String key : [Link]()) { }
[Link]("k"); for (int val : [Link]()) { }
[Link](1);
[Link]("k");
[Link](); [Link]();

Frequency count pattern Compute helpers


[Link]("k", [Link]("k",0)+1); [Link]("k", new ArrayList<>());
[Link]("k", k->new
ArrayList<>()).add("v");
[Link]("k", 1, Integer::sum); // add 1 or init to 1

3. List — Common Operations


Add / Remove / Access Sort / Slice
[Link]("a"); [Link](list);
[Link](0, "a"); // insert at index [Link](list, [Link]());
[Link](0); [Link](list);
[Link](0, "b"); // update [Link]((a,b)->[Link]()-[Link]());
[Link](0); // by index [Link](0, 3); // [0,3) exclusive end
[Link]("a"); // by value [Link](list);
[Link](); [Link](); [Link](); [Link](list); [Link](list);
[Link]("a"); [Link]("a");

4. String — Common Operations


Basic Convert
[Link](); [Link](42); // int -> String
[Link](0); [Link]("42"); // String -> int
[Link](1, 3); // "el" end exclusive [Link]("3.14");
[Link]("l"); // 2 char[] c = [Link]();
[Link]("ell"); new String(charArray);
[Link]("he"); [Link]("lo"); [Link]().toArray(); // int[] of ASCII
[Link]("hello"); // NEVER use ==
[Link]("HELLO");
[Link]("l","r");
[Link](); [Link]();
[Link](); [Link]();
Split / Join StringBuilder
[Link](","); // String[] StringBuilder sb = new StringBuilder();
[Link](",", -1); // keep trailing empty [Link]("hello"); [Link](42);
[Link](",", 2); // limit splits [Link](0, "prefix");
[Link](",", list); // join list [Link]();
[Link]("-", "a","b","c"); [Link](0);
[Link](0, 3);
[Link]();
[Link]();
[Link](0); [Link](0,'x');

5. Arrays — Common Operations


Declare / Init Copy / Fill / Print
int[] arr = new int[5]; [Link](arr, 0);
int[] arr = {1,2,3,4,5}; [Link](arr, [Link]);
int[][] grid = new int[3][4]; [Link](arr, 1, 4); // [1,4) exclusive
int[][] grid = {{1,2},{3,4}}; [Link](arr); // "[1, 2, 3]"
[Link]; // NOT [Link]() [Link](grid); // 2D

Sort / Search Convert to/from List


[Link](arr); // int[] -> List<Integer>
[Link](arr, 0, 3); // sort range [0,3) List<Integer> list = [Link](arr)
[Link](strArr, (a,b)->[Link](b)); .boxed().collect([Link]());
int idx = [Link](arr, 3); // must be sorted // List<Integer> -> int[]
int[] arr = [Link]()
.mapToInt(Integer::intValue).toArray();
// Integer[] -> List
List<Integer> list = [Link](integerArr);

6. Queue / Stack / Deque / PriorityQueue


Queue (FIFO) Deque (both ends)
Queue<Integer> q = new LinkedList<>(); [Link](1); [Link](1);
[Link](1); // add to tail [Link](); [Link]();
[Link](); // remove head (null if empty) [Link](); [Link]();
[Link](); // view head (null if empty)
[Link](); [Link]();

Stack (LIFO) — use Deque PriorityQueue


Deque<Integer> stack = new ArrayDeque<>(); PriorityQueue<Integer> pq = new PriorityQueue<>(); //
[Link](1); // add to top min
[Link](); // remove top PriorityQueue<Integer> pq = new PriorityQueue<>(
[Link](); // view top [Link]()); // max
[Link](5); [Link](); [Link]();
// Custom: sort int[] by index 1
PriorityQueue<int[]> pq = new
PriorityQueue<>((a,b)->a[1]-b[1]);

7. Math, Character & Type Tricks


Math Character
[Link](a,b); [Link](a,b); [Link](-5); [Link]('5');
[Link](2,10); // 1024.0 [Link]('a');
[Link](16); // 4.0 [Link]('a');
[Link](7.0/2); // 3.0 [Link]('A');
[Link](7.0/2); // 4.0 [Link](' ');
(int)(7.0/2); // 3 (truncates) [Link]('A');
Integer.MAX_VALUE; // 2147483647 [Link]('a');
Integer.MIN_VALUE; // -2147483648 'a' - 'a'; // 0 — char arithmetic
Long.MAX_VALUE; 'z' - 'a'; // 25
(int)'A'; // 65 ASCII value
(char)65; // 'A'

8. Common Patterns — Stripe & FAANG


Frequency Count
Map<Character,Integer> freq = new HashMap<>();
for (char c : [Link]())
[Link](c, [Link](c, 0) + 1);

Group by Key
Map<String,List<String>> groups = new HashMap<>();
[Link](key, k -> new ArrayList<>()).add(value);

Sliding Window
int left = 0;
for (int right = 0; right < n; right++) {
// expand: add [Link](right) to window
while (/* window invalid */) { left++; }
result = [Link](result, right - left + 1);
}

Two Pointers
int left = 0, right = n - 1;
while (left < right) {
// process arr[left] and arr[right]
if (condition) left++;
else right--;
}

BFS Template
Queue<Integer> q = new LinkedList<>();
Set<Integer> visited = new HashSet<>();
[Link](start); [Link](start);
while (![Link]()) {
int node = [Link]();
for (int neighbor : [Link](node)) {
if (![Link](neighbor)) {
[Link](neighbor);
[Link](neighbor);
}
}
}

Sort Map by Value (descending)


[Link]().stream()
.sorted([Link].<String,Integer>comparingByValue().reversed())
.forEach(e -> [Link]([Link]()+":"+[Link]()));

9. Stripe-Specific Patterns
Parse CSV / structured input Check numeric string
String[] parts = [Link](",", -1); // keep empty boolean isNum = [Link]("-?\\d+(\\.\\d+)?");
String from = parts[0].trim(); // Or safer:
String to = parts[1].trim(); try { double d = [Link](str); }
double amt = [Link](parts[2].trim()); catch (NumberFormatException e) { /* not numeric */ }
// Skip header
boolean first = true;
for (String line : lines) {
if (first) { first=false; continue; }
// process line
}

Nested graph (currency / shipping) Fixed-width length prefix parser


Map<String,Map<String,Double>> graph = new HashMap<>(); int i = 0;
[Link](from, k->new HashMap<>()).put(to, while (i + 2 <= [Link]()) {
rate); int len = [Link]([Link](i, i+2));
// Add reverse edge if (len == 0) break;
[Link](to, k->new HashMap<>()).put(from, i += 2;
1.0/rate); [Link]([Link](i, i+len));
i += len;
}
Java Interview Cheatsheet • Keep handy during your Stripe interview • Good luck! ■

You might also like