Beaconfire Interview Experience
Beaconfire Interview Experience
Part I
e.
Overloading and overriding
Overloading : If two or more methods have same name, but different argument then it is called method overloading.
Number of Arguments | Overloaded method can have different number of arguments |
---|---|
Date type | Overload method can have different data type for argument |
Return type | Return type can be changed but either number of argument or data type of argument should also be changed. |
Order of arguments | If you change sequence of arguments then it is also valid method overloading provided you have different data types arguments. |
Constructor | Can be overloaded |
Overriding:
If subclass is having same method as base class then it is known as method overriding Or in another words.
If subclass provides specific implementation to any method which is present in its one of parents classes then it is known as method overriding.
Arguments | Must not change |
---|---|
Return type | Can’t change except for covariant (subtype) returns |
Access Modifier | Must not be more restrictive. Can be less restrictive. |
Exceptions | Can reduce or eliminate but must not throw new/broader checked exceptions |
Constructor | Can not be overridden |
Static method | Can not be overridden |
final method | Can not be overridden |
array list vs linked list
Linked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part.
The elements are linked using pointers and addresses.
Each element is known as a node.
Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays.
Array List is a part of the collection framework.
Unlike arrays, arraylists can automatically adjust its capacity when we add or remove elements from it. Hence, arraylists are also known as dynamic arrays.
It is present in the java.util package and provides us dynamic arrays in Java.
Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
We can dynamically add and remove items. It automatically resizes itself.
Difference
ArrayList | LinkedList |
---|---|
This class uses a dynamic array to store the elements in it. With the introduction of generics, this class supports the storage of all types of objects. | This class uses a doubly linked list to store the elements in it. Similar to the ArrayList, this class also supports the storage of all types of objects. |
Manipulating ArrayList takes more time due to the internal implementation. Whenever we remove an element, internally, the array is traversed and the memory bits are shifted. | Manipulating LinkedList takes less time compared to ArrayList because, in a doubly-linked list, there is no concept of shifting the memory bits. The list is traversed and the reference link is changed. |
This class implements a List interface. Therefore, this acts as a list. | This class implements both the List interface and the Deque interface. Therefore, it can act as a list and a deque. |
This class works better when the application demands storing the data and accessing it. | This class works better when the application demands manipulation of the stored data. |
array vs array list
An array is a collection of items stored at contiguous memory locations.
- The idea is to store multiple items of the same type together.
- However, the limitation of the array is that the size of the array is predefined and fixed.
Difference
- An array is basic functionality provided by Java. ArrayList is part of collection framework in Java. Therefore array members are accessed using [], while ArrayList has a set of methods to access elements and modify them.
- Array is a fixed size data structure while ArrayList is not. One need not to mention the size of Arraylist while creating its object. Even if we specify some initial capacity, we can add more elements.
- Array can contain both primitive data types as well as objects of a class depending on the definition of the array. However, ArrayList only supports object entries, not the primitive data types.
Note: When we do arraylist.add(1); : it converts the primitive int data type into an Integer object. - Since ArrayList can’t be created for primitive data types, members of ArrayList are always references to objects at different memory locations (See this for details). Therefore in ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations.
In array, it depends whether the arrays is of primitive type or object type. In case of primitive types, actual values are contiguous locations, but in case of objects, allocation is similar to ArrayList. - Java ArrayList supports many additional operations like indexOf() in java/), remove(), etc. These functions are not supported by Arrays.
HashMap
It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs, and you can access them by an index of another type (e.g. an Integer). One object is used as a key (index) to another object (value). If you try to insert the duplicate key, it will replace the element of the corresponding key.
Implement
handle collisions
closed addressing(open hashing)
In Open Hashing each cell in the array points to a list containg the collisions. The hashing has produced the same index for all items in the linked list.
open addressing(close hashing)
In Closed Hashing you use only one array for everything. You store the collisions in the same array. The trick is to use some smart way to jump from collision to collision until you find what you want. And do this in a reproducible / deterministic way.````
1 | public class MyHashMap { |
Part II
Destination City
Next Greater Element I
Number of Longest Increasing Subsequence