Professional Documents
Culture Documents
Interview Notes
Interview Notes
The size, isEmpty, get, set, iterator, and listIterator operations run in constant
time. The add operation runs in amortized constant time, that is, adding n elements
requires O(n) time. All of the other operations run in linear time (roughly
speaking). The constant factor is low compared to that for the LinkedList
implementation.
Operations:
Insert
Delete
Search
Navigate
Linked List:
===========================================================================
For building linked list,we need to have a class called Node.
Node{
Object value; // placeholder for the value
Node nextNode; // Reference to the next node
}
no of nodes in the lower level = all higher level nodes+1; (since half of the
nodes are in the lower level, its efficient to search)
no of nodes in a perfect binary tree = 2^(level).
0the level -> 2^0 = 1.
1st level -> 2^1 = 2;
for binary tree its important to have it balanced, otherwise complexity would
turn to O(n)
Advantages:
All nodes have either 0 or 2 nodes but leaf nodes exists in different levels
These trees auto balance if it finds if one side is growing bigger. Java uses self
balancing trees in the hash map collision scenarios.
1. AVL Trees
https://medium.com/basecs/the-little-avl-tree-that-could-86a3cae410c7
2. Red/Black Trees:
Advantages:
Better than O(n)
Priority
Flexible Size
Fast Insert
Disadvantages:
Its used to store the words with the letters. Best way for dictionary like things,
autocompletion, Find Number of words with a letter "A" etc.
A B
N P A
T D P T
L
E
Graphs:
=========================================
Cyclic -> have a path in the graph back to same node
Acyclic -> No cycles
Undirected -> we can traverse both directions, no specific direction
Directed -> Only in specified direction. Though some may look cyclic they may not
have a cycle due to direction
Weighted -> We can add some weight on the edge of the graph which can use useful
for shortest path algo.
Advantages:
Dissadvantages:
Scaling is very hard
Recursion
=========================================================
1. Identify base case => when the recursive call should end and return the result
2. Identify recursive case => when should we contiune making the method calls
3. Get Closer and closer to the base case and return when needed.
Recursive methods would be nested in the callStack. So the final return value from
the base value should be propagated to all the earlier calls. So for that purpose
we need to add return for recursive calls also.(Very Important)
Every time you are using a tree or converting something into tree consider
recursion.
1. Divded into number of sub problems that are smaller instances of the same
problem.
2. Each Instance of the subprobelm is identical in nature
3. The solutions of each subproblem can be combined to solve the problem at hand
Time Complexity:
=========================================================================