APIs To Learn Flashcards

1
Q

vLeast Integer

A

Integer.MIN_VALUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Greatest Integer

A

Integer.MAX_VALUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Greater of two numbers

A

Math.max(double, double)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

add collection to other collection

A

::addAll(Collection c)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

char[] from String

A

String::toCharArray

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

check if char is numeric

A

Character.isDigit(char)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Eulerian Path on Undirected Graph

A

Either every vertex has even degree
-or-
exactly two vertices have odd degree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Eulerian Circuit on Undirected Graph

A

Every vertex has an even degree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Eulerian circuit on directed graph

A

Every vertex has an equal in degree and out degree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Eulerian Path on Directed Graph

A

At most 1 vertex with one more out degree than in degree
- and -
at most 1 vertex with one more in degree than out degree
- and -
all other vertices have equal in and out degrees

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

number of chars in string

A

String::length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

get “bcde” from “abcde”

A

“abcde”.substring(1) // NOTE ALL LOWER CASE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

get “bcd” from “abcde”

A

“abcde”.substring(1,4) // begin index is inclusive, end index is not inclusive NOTE ALL LOWER CASE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

insert element into Set

A

Set.add(element)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Get any element from Set

A

Set::iterator::next

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

concrete implementation of stack

A

Stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Boxed Object for char

A

Character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

number of elements in list

A

List::size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

sort int[] (and complexity)

A

Arrays.sort(int[] a), O(n log(n))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

copy an int[]

A

Arrays.copyOf(int[] original, int newLength)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

sort int[] range (and complexity)

A

Arrays.sort(int[] a, int fromIndex, int toIndex) O(n log(n)), fromIndex is inclusive, toIndex is exclusive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

parse int value from char

A

Character.getNumericValue(c)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

check if map has any key value mappings

A

Map::isEmpty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

remove key value mapping from map

A

Map.remove(Object key)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

put element in set

A

Set.add(Object o)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

increment a maybe null map key with default

A

int count = m.getOrDefault(k, d);
m.put(k, count + 1);

or

m.compute(k, (k, count) -> count == null ? 1 : count+1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

int to String

A

String.valueOf(int n) or specific implementation Integer.toString(int n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Data Structure that maintains unique elements with order

A

TreeSet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

least element in TreeSet

A

treeSet::first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

greatest element in TreeSet

A

treeSet::last

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

TreeSet least element strictly greater than the given element (or null if not exist)

A

treeSet.higher(E e)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

TreeSet greatest element strictly less than the given element, or null if there is no such element.

A

treeSet.lower(E e)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Queue that maintains order based on Comparator

A

new PriorityQueue(Comparator comparator)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Comparator Signature

A
int compare(T o1, T o2)
Returns: a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Look at top of Stack without removing

A

Stack::peek

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

square root

A

Math.sqrt(double a)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

raise to power

A

Math.pow(double a, double b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Integer Comparator

A

Integer::compare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Array Literal

A

new int[]{-1, -1}

40
Q

get int[][] from List

A

list.toArray(new int[0][]); using new int[0][] is strangely much faster than a sized array: https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusion

41
Q

Disjoint Set

A
Node class with Union and Find operations, can track tree number with external variable
Union / Find may be linear with a naïve implementation, but with union by rank and path compression are effectively constant
42
Q

Disjoint Set: union by rank

A

every node starts with rank 1.
Whenever nodes are unioned, if their rank is equal increment the root rank and add the other under it.
In all other cases, just add the lesser under the greater.

43
Q

Disjoint Set Path Compression

A

in find operator, if the node’s parent is a root node, return that parent. Otherwise, return the assignment:
return this.parent = this.parent.find();

44
Q

Create comparator for object property

A

Comparator.comparing(Function keyExtractor)

45
Q

Reverse a comparator

A

comparator.reversed()

46
Q

combine comparators

A

comparator.thenComparing(Comparator other)

47
Q

What happens when Map::get missing key

A

return null

48
Q

max signed int in scientific notation

A

2*10^9

49
Q

max signed long in scientific notation

A

9*10^18

50
Q

Map implementation that maintains order of keys (and time costs)

A

TreeMap log(n) time cost for the containsKey, get, put and remove

51
Q

TreeMap least key greater than or equal to the given key

A

treeMap.higherKey(K key) (or null if not exist) (also higherEntry)

52
Q

TreeMap greatest key less than or equal to the given key

A

treeMap.lowerKey(K key) (or not exist) (also lowerEntry)

53
Q

Breadth First Search

A

Iterate with Queue, check for visited with HashSet (or visited property)
time complexity O(v+e)
space complexity O(v)
can optimize with dual start (but same time complexity)

54
Q

Queue concrete implementation

A

LinkedList

55
Q

Depth First Search

A

iterate with recursion (or stack), check for visited with HashSet (or visited property)
time complexity O(v+e)
space complexity O(v) but in practice can be less than BFS (e.g. tree is log(v))

56
Q

Topological Sort

A

Kahn’s Algorithm
repeatedly remove nodes with 0 indegree from graph, add them to the ordering, and enqueue children with newly 0 indegree to queue for subsequent removal

57
Q

count of leafs in perfect binary tree (or number at level) where height at root is 0

A

2^height

58
Q

count of nodes in perfect binary tree where height at root is 0

A

2^(height + 1) - 1

59
Q

binary search

A

select a left and right window
while left <= right, select middle pivot
bring left or right in depending on which side of pivot

60
Q

find Eulerian Path / Circuit

A

Hierholzer’s algorithm
start from valid starting node (for path, outdegree greater)
dfs (with recursion) until run out of outgoing edges (mark visited)
backtrack up the recursion stacks adding edges to solution until find an unvisited outgoing edge, then replay from there

61
Q

Minimum Spanning Tree

A

Kruskal’s Minimum Spanning Tree Algorithm
Sort the edges by weight
iterate through smallest edge. Check if it forms a cycle with the spanning tree formed so far (union find). If roots are unique, include this edge. Else, discard it.
repeat until v-1 edges

62
Q

Closed form for sum of numbers from 0 to n

A

Gauss’ Formula: n(n+1)/2

63
Q

find entrance to cycle in linked list

A

Floyd’s Tortoise and Hare:
start two pointers at head, jump the hare 2x as fast
if there is a cycle, they will meet
if they meet, reset one index and then increment both at 1x speed
the index of the second meeting is the entrance to the cycle

64
Q

Triangle Number

A

n (n+1) / 2

65
Q

Java map key type with 2 instead of 1 input

A

Pair<>

66
Q

how to create hashmap that doesn’t get rehashed and why

A

new HashMap( (int) maxCapacity / 0.75), because 0.75 is the default load factor and rehash happens at capacity * load factor

67
Q

initialize int array with size

A

new int[size]

68
Q

check if character is alphabetic

A

Character.isLetter(char c)

69
Q

check if character is upper case

A

Character.isUpperCase(char c)

70
Q

check if character is lower case

A

Character.isLowerCase(char c)

71
Q

change character to upper case

A

Character.toUpperCase(char c)

72
Q

lower case version of character

A

Character.toLowerCase(char c)

73
Q

remove last character from StringBuffer

A

StringBuffer.deleteCharAt(sb.length()-1)

74
Q

create list from int[]

A

Arrays.stream(nums).boxed().collect(Collectors.toList())

75
Q

swap elements in list

A

Collections.swap(List list, int i, int j)

76
Q

count of elements in triangle number for n

A

n (n+1) / 2

77
Q

remove last element of (java standard library) LinkedList

A

LinkedList::removeLast

78
Q

formula for number of sub-combinations of size r from set with size n

A

n! / r! (n-r)! recursively multiply the options * options-1 up to set size but also divide by orderings

79
Q

formula for number of permutations of size r from n distinct objects

A

n! / (n-r)! recursively multiply the options * options-1 up to set size

80
Q

formula for ways to arrange n objects

A

n!

81
Q

formula for number of subsets in a set with size n

A

2^n for every element, you either include it or you don’t

82
Q

number of chars in stringbuffer

A

StringBuffer::length

83
Q

delete string range from StringBuffer

A

StringBuffer.delete(int start, int end)

84
Q

get element from array that occurs more than length / 2 times

A

Boyer-Moore Voting Algorithm
maintain a candidate and a count
for each elemenet, increment the count if equal else decrement the count
if count hits 0, reset with next candidate
run second time to check full count (could be less than 1/2)

85
Q

copy range from java array to new array

A

Arrays.copyOfRange(int[] array, int from, int to) where to is exclusive

86
Q

Merge Sort Algorithm

A

if length is 1, return self
else split into halves
recursively merge sort both halves
combine the halves

87
Q

Quick Sort Algorithm

A

if list size is 1, return list
else pick an element as pivot (right most is convenient)
rearrange the list such that everything greater than pivot is to the right of pivot (swapping and shifting pivot)
recursively call quick sort on left side and right side (if exist)

87
Q

Quick Sort Algorithm

A

if list size is 1, return list
else pick an element as pivot (right most is convenient)
rearrange the list such that everything greater than pivot is to the right of pivot (swapping and shifting pivot)
recursively call quick sort on left side and right side (if exist)

88
Q

get entries from hashmap

A

HashMap::entrySet

89
Q

empty out a set

A

Set::clear

90
Q

get subarray of array

A

Arrays.copyOfRange(T[] array, int beg, int end)

91
Q

first n elements from stream

A

Stream<>.limit(int n)

92
Q

sort a stream

A

Stream<>.sorted(Comparator)

93
Q

Stream to int[]

A

stream.mapToInt(i -> i).toArray();

94
Q

retain only unique elements in stream

A

Stream::distinct