Important Flashcards

1
Q

What does map.getOrDefault(k, default v) method do?

A

If the key exists, returns its value
Otherwise, return the default value

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

What does map.entrySet() do?

A

Returns the map as a Set view

Ie.,
HashMap: {Top=10, Shoes=20}
Set View: [Top=10, Shoes=20]

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

What does the toArray() method do to a LinkedList?

A

Converts the Linked List object into an array of object type and returns it

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

How do you check if a map contains a key or a value?

A

use map.containsKey(k)
or
map.containsValue(v)

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

What does the Hash Map values() method do?

A

Gets the value set

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

List 11 examples of HashMap methods

A

put(key, value)
get(key)
getOrDefault(key, defaultValue)
remove(key)

containsKey(key)
containsValue(value)

keySet()
entrySet()

clear()
size()
isEmpty()

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

How do you get the length of a string?

A

string.length()

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

How do you get a character at a given index of a string?

A

string.charAt(i)

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

How many ASCII characters are there?

A

There are 128 ASCII characters, but there is also an extended version of 256 ASCII characters

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

How many letters are in the alphabet?

A

26

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

How could you find the longest substring without repeated characters?

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

What is the Fibonacci sequence?

A

Each number is the sum of the 2 preceding numbers starting from 0 and 1

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

What data structure is an efficient way to get a Fibonacci number and why?

A

Defining a Hash Map outside the method because it can cache the results

public int fib(int n) {
if(n<-1){
return n;
}
if(map.containsKey(n)){
return map.get(n);
} else {
int result = fib(n-1) + fib(n-2);
map.put(n, result);
return result;
}
}

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