Interview Flashcards

1
Q

What are access modifiers?

A

Keywords that can be used to control the visibility of field methods

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

What are 4 access modifiers?

A

public
private
protected
default

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

What is a hash map?

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

What is a private access modifier?

A

can only access from within the same class

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

What is a default access modifier?

A

can only access from within the same package and not from outside

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

What is a protected access modifier?

A

can access from within the same package and from outside the class with the help of a child class

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

What is a public access modifier?

A

Access from anywhere?

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

Can you explain what your understanding of abstraction is?

A
  • hiding complexities of implementation and exposing simpler interfaces
  • abstract class
  • interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a circular array, and how is it different from a regular array?

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

How do you implement a circular array in Java?

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

How do you insert an element into a circular array?

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

How do you remove an element from a circular array?

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

How do you traverse a circular array?

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

What is 3%2

A

2 goes into 3 once and 1 is leftover

1

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

What is 15%4?

A

4 goes into 15 3 times to give us 12

then to get the remainder we do 15-12 = 3

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

Give 2 examples of what you can use modulus operator for

A

To check if N is divisible by M (for example, odd or even) or N is a multiple of M

To work out how many positions to move in a circular array

17
Q

What does the volatile keyword mean in Java?

A

The volatile keyword is used as a modifier for variables.

A variable declared as volatile may be modified by different threads running in the program

volatile makes sure that any changes to the variable are immediately visible to all threads (otherwise modifications made by 1 thread in a multi-threaded environment might not be immediately visible to the other threads due to caching and optimisation mechanisms used by modern processors)

18
Q

When would you use an ArrayList instead of a LinkedList?

A

When you want to access elements by their index frequently and not just from the end / beginning (then the linked list would be better)

19
Q

What is the underlying implementation of an ArrayList?

A

An ArrayList is a resizeable array

Internally it contains an array of objects that automatically increase/decrease as elements are added and removed from the list

If you add more elements than the current capacity typically the internal array will double in size and the existing elements are copied to a new, larger array

This resizing is an expensive operation so it is important to choose an appropriate initial capacity

When you add an element, it is added to the end of the array and the size of the list increases by 1

When you remove an element, the elements to the right of the removed element shift to the left

Array Lists are not synchronized so you need to use Collections.synchronizedList

20
Q

What is the underlying implementation of a LinkedList?

A

A LinkedList is implemented as a doubly linked list. A doubly linked list consists of a sequence of nodes, where each node contains a reference to the previous node and the next node in the list. The first and last nodes in the list are often referred to as the “head” and “tail” of the list.

When you create a LinkedList, it starts out empty. To add an element to the list, a new node is created with a reference to the previous node (which is the current tail of the list), and the tail of the list is updated to point to the new node. When you remove an element from the list, the node is unlinked from the previous and next nodes in the list, and the previous and next nodes are updated to point to each other.

One of the advantages of a LinkedList over an ArrayList is that inserting or removing elements in the middle of the list is relatively fast, because it only requires updating a few references to nodes, whereas in an ArrayList, inserting or removing an element in the middle requires shifting all the subsequent elements in the array.

However, accessing elements in a LinkedList by their index can be slower than in an ArrayList, because it requires traversing the list from the beginning or end until the desired element is reached. Additionally, because LinkedList nodes are scattered throughout memory, iterating over the list can result in many cache misses, which can impact performance.

It is important to note that LinkedList is not thread-safe by default, and if you need to use it in a multi-threaded environment, you should either use synchronization or use a thread-safe alternative like java.util.concurrent.ConcurrentLinkedDeque.

21
Q

What is the underlying implementation of a Hash Map?

A

In Java, a HashMap is implemented as a hash table, which is a data structure that supports constant-time average-case operations for inserting, retrieving, and removing key-value pairs.

Internally, a HashMap is an array of buckets, where each bucket is a linked list of entries. When you put a key-value pair into the map, the key is hashed to an integer value that is used as an index into the array. If there is no entry at that index, a new entry is created and added to the linked list in that bucket. If there is already an entry at that index with the same key, the value associated with that key is replaced with the new value.

To retrieve a value from the map, the key is again hashed to an integer, which is used as an index into the array. The linked list in the corresponding bucket is then searched for an entry with the matching key. If a matching entry is found, its value is returned. If no matching entry is found, null is returned.

The load factor of the HashMap determines when the internal array is resized. The default load factor is 0.75, which means that the array is resized when the number of entries in the map exceeds 75% of the current capacity. When the array is resized, the number of buckets is doubled, and the entries are rehashed to their new indices. Resizing the array can be an expensive operation, so it is important to choose an initial capacity that is appropriate for your use case.

It’s worth noting that the order of the entries in a HashMap is not guaranteed, because the order of the entries in the linked lists within each bucket depends on the hash codes of the keys, which are not necessarily ordered. If you need to maintain the order of the entries, you can use a LinkedHashMap, which is a subclass of HashMap that maintains a doubly linked list of entries in insertion order.

22
Q

How do you minimise the number of collisions?

A
23
Q

How do you create an object with multiple keys so that you can pass it to the hash function as the key?

A
24
Q

What is an access modifier?

A

Access modifiers are used to enforce encapsulation
- public
- private (only accessed and modified from within the class that defines them)
- protected (same class, subclasses of the same class and others within the same package)
- default (same package, not outside the package)

25
Q

When is an ArrayList a good choice?

A

An ArrayList is a good choice when you want to access elements in the list frequently

O(1) time to access elements by their index

ie., to store a amount of data that needs to be accessed and manipulated frequently, such as a list of stock prices or a list of employee salaries

26
Q

When is a LinkedList a good choice?

A

A LinkedList is a good choice when you frequently need to add or remove elements from the beginning or end of the list, but don’t need to access elements by index frequently

ie., you might use a LinkedList to implement a queue or a stack, or to store a list of user input events that need to be processed in the order they were received.

27
Q

How do you minimise the number of collisions in a Hash Map?

A

A collision in a HashMap occurs when two or more keys hash to the same index in the underlying array. Collisions can impact the performance of the HashMap, because the linked list in the corresponding bucket must be searched for the correct entry.

To minimize the number of collisions in a HashMap, you can take the following steps:

Choose a good hash function: A good hash function should distribute the keys evenly across the indices in the array. Java’s Object class provides a hashCode() method, which is used by default to hash keys in a HashMap. However, the default implementation may not be sufficient for your use case. You can provide your own hash function by overriding the hashCode() method in the key’s class.

Choose an appropriate initial capacity: The initial capacity of a HashMap determines the number of buckets in the underlying array. Choosing an appropriate initial capacity can help to reduce collisions. If you know the approximate number of key-value pairs that will be stored in the map, you can set the initial capacity to a value that will result in a reasonable load factor.

Choose an appropriate load factor: The load factor of a HashMap determines when the internal array is resized. A lower load factor will result in more buckets in the array, which can help to reduce collisions. However, a lower load factor also means that the array will be resized more frequently, which can impact performance.

Use keys with good distribution: If possible, choose keys that have a good distribution across the entire range of possible values. For example, if you are using integers as keys, you can use prime numbers as the capacity of the HashMap and the hash function to minimize collisions.

By taking these steps, you can help to minimize the number of collisions in a HashMap and improve its performance.

28
Q

What are the 4 pillars of object oriented programming?

A

Inheritance, Polymorphism, Encapsulation and Abstraction

29
Q

What is NOSQL?

A
30
Q

How is NOSQL different from SQL?

A
31
Q

Describe Springboot?

A
32
Q

What are the key advantages of using Springboot?

A
33
Q

What is the annotation for a spring dependency injection?

A
34
Q

Tell me about a time you have taken a risk?

A
35
Q

When should you use NOSQL?

A