Coding Interview Questions Flashcards
Copy char[] to String
- char[].toString()
- new String(char[])
- String.valueOf(char[])
- new String(char[])
3. String.valueOf(char[])
Which one of the two is correct?
- int[] sequence = new int[]{0, 1};
- int[] sequence = {0, 1};
Both 1 and 2 are correct.
- int[] sequence = new int[]{0, 1};
- int[] sequence = {0, 1};
Initialize a List with values:
- ArrayList list = new ArrayList(); list.add(item1);
- ArrayList list = new ArrayList() { item1 };
- ArrayList list = new ArrayList();
list.add(item1);
public class Test {
class A { }
static class B { }
public static void main(String[] args) {
A a = new A();
B b = new B();
}
}
1. A instantiation will compile, no instance of Test is needed to instantiate A
2. B instantiation will compile, no instance of Test is needed to instantiate B
3. B will fail, you need an instance of Test to instantiate B
4. A will fail, you need an instance of Test to instantiate A
- B instantiation will compile, no instance of Test is needed to instantiate B
- A will fail, you need an instance of Test to instantiate A
Get List size given List list;
- list.length;
- list.size();
- list.size()
Initialize a List:
- List nodes = new ArrayList<>();
- List nodes = new List<>();
- List nodes = new ArrayList<>();
Which one is correct?
- O(nlog(n))
- O(n log n)
- O(nlog(n))
How to sort an ArrayList?
- arrayList.sort()
- Collections.sort(arrayLIst);
- Collections.sort(arrayLIst);
Get character counts from map Map
- map.merge(chr, 1, Integer::sum);
- map.put(chr, map.getOrDefault(chr, 0) + 1);
Both 1 and 2 are correct
Traverse Character type chars in string
- for (int i = 0; i < s.length(); i++) { s.charAt(i); }
- s.chars().forEach(chr -> { })
- for (Character chr : s) { }
- for (int i = 0; i < s.length(); i++) { s.charAt(i); }
Add key value to map
- map[key] = value;
- map.put(key, value);
- map.put(key, value);
The ‘a’ char code is
- 79
- 97
- 48
- 84
- 97
The ‘0’ (zero) char code is
- 48
- 84
- 56
- 65
- 48
The ‘A’ char code is
- 97
- 79
- 65
- 56
- 65
The best conceivable runtime means
- it is the best time you can conceive that the problem could possibly be solved in, and it is definitely impossible for the problem to be solved faster than that.
- you have an algorithm that solves the problem, and in the best case, that algorithm has a particular time complexity.
- you are feeding the worst possible input (of that size) into your algorithm
- it is the best time you can conceive that the problem could possibly be solved in, and it is definitely impossible for the problem to be solved faster than that.
String input = …;
- char chr = Input.charCodeAt(4);
- int chr = Input.charCodeAt(4);
- char chr = input.charAt(4);
- int chr = input.charAt(4);
- char chr = input.charAt(4);
Select valid statements
- Int codePoint = fifthCharl
- int codePoint = (int) fifthChar
- char opp = (char);
- int opp = (char);
- int codePoint = (int) fifthChar
3. char opp = (char);
Are the following expressions valid? (Yes/No)
- ‘z’ - ‘a’
- ‘a’ + int
Yes
Why is the following valid? char c = 'a' + 10; But, not the following? int i = 10; char c = 'a' + i;
Because the compiler can check that it (‘a’ + 10) is within the bounds of a char whereas it cannot (in general) check that ‘a’ + is within the bounds.
What is the result of evaluating the following?
26 % 26
- 26
- 0
- 1
- 0
What is the result of evaluating the following? 27 % 26 1. 26 2. 0 3. 1
- 1
What is the result of evaluating the following? 25 % 26 1. 25 2. 0 3. 1
- 25
- array.length;
2. array.length();
- array.length;
Math.floor returns
- integer
- double
- float
- double
- MIN_VAL
- Integer.MIN_VALUE
- Integer.MIN_VAL
- Integer.MIN_VALUE
- new Map<>{};
- new Map<>();
- new HashMap<>{};
- new HashMap<>();
- new HashMap<>();
- map.contains(key);
- map.has(key);
- map.containsKey(key);
- map.containsKey(key);
- static method cannot be referenced from a static context
- non-static method cannot be referenced from a non-static context
- non-static method cannot be referenced from a static context
- static method cannot be referenced from a non-static context
- non-static method cannot be referenced from a static context
In a graph
- vertices are the nodes and edges are the connections
- vertices are the connections and edges are the nodes
- vertices are the nodes and edges are the connections
- Trees are graphs that contain cycles.
2. Trees are graphs that do not contain even a single cycle.
- Trees are graphs that do not contain even a single cycle.
In a graph, acyclic refers to:
- contains no cycles
- contains cycles
- contains no cycles
DFS time complexity in a tree is:
- O(V + E)
- O(V)
- O(V^2)
- O(V)
DFS time complexity in a graph is;
- O(V + E)
- O(V)
- O(V^2)
- O(V + E)
List is interchangeable with ArrayList?
- True
- False
- False
Object element
- element instanceof ArrayList
- element typeof ArrayList
- element instanceof ArrayList
Cast Object to List given: Object obj;
- Arrays.asList(obj)
- (List) obj
- (List) obj
Which one is considered better?
- List myList = new ArrayList();
- ArrayList myList = new ArrayList();
- List myList = new ArrayList();
Which one is correct to transform a resizable list of chars into a String? 1. List chars = new ArrayList<>(); chars.add(chr); return new String(chars); 2. StringBuilder chars = new StringBuilder(); chars.append(chr) return chars.toString();
- StringBuilder chars = new StringBuilder();
chars.append(chr)
return chars.toString();