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.