Data structures and algorithm

 Data structures and algorithm


Name tree searching algorithms? 
  • Depth first search - It starts with the root node and first visits all nodes of one branch as deep as possible of the chosen Node and before backtracking, it visits all other branches in a similar fashion.
  • Breadth-First Search (BFS) Algorithm: It also starts from the root node and visits all nodes of current depth before moving to the next depth in the tree.


what are the data structure do u know?
List, set,queue,map,Araylist,Tree,Stack,queue
Difference between array and array list? array lenth is fixed while arraylist can grow dinamically.

What is vector?
vector is a slower arraylist. use for web applications to pass data.


Collection Hierarchy





List 
  • Ordered
  • support index based search
  • Include duplicated
SET
  • Not orderd
  • not suport index based search
  • Does not allow duplicate

Queue
  • FIFO aproach
  • elements add to rear end and remove from front end
Map
  • Key value pair
  • Map interface does not implement the collection interface
  • unique key
  • allow duplicates
Array list
  • Dynamic resizing, when need to increases from 1 then it increase 50% of the exisitng size
  • Non syncronized

Linked list
  • Maintain insersion order
  • non synchronized
  • does not suport acess elements randomely
  • Can use listiterater to iteration

vector
  • Synchornized
  • Thresdsafe
  • Maintain insertion order
  • when the size completed it double the size
stack
  • Last in first out
  • Elements are added and remove from rear end
Hashset
  • Implements Hashtable
  • unique elements
  • one null element allow
  • unorders
Linked Hash set
  • Ordered version of hash set
  • preserve the insertion order

Sorted Set
  • Set sorted in acesnding order
  • all element must implemet comparable interface
Treee Set
  • Faster
  • Sorted in acesending order

Priority queue - high priority elements served before the low priority irespective of the insersion order
deque - Elements can be added /remove from either end
Arrayqueue - resizable array inadition to the implemtation of deque


HashMap - 
  • Non syncronized
  • allow one null key multiple null values
Hashtable
  • synchronized
  • not allow null key or null value
sorted map
  • Ascending order
  • cannot store null key

Why Map is not extend collection framework
Map interface is allow for key value pair structure whereas the collection interface is a collection of objects
collection interface is that the add(E e) and it does not suport to add key value pair like put(K,V)


Fail-fast and fail-safe iterators

Fail fast Iterator throws concurrentModification exception whereas fail-safe iteraters does not throw concurrentmodification exception becaue it work on clone of the collection instead of original collection.

BLockingque can be acess by multiple thread without any concurrency issue.. Its capable of blocking the threads that try to insert or take elements from the queue.

Difference between Synchronized and concurrent collection
  • Both are thred safe but difference comes in performance and scalability
  • Synchronized hashmap are slower than their concurrent Hashmap due to the locking in syncronized collection.
HAshMap works on hashing principle where hash function are used to link key and value in hashMap.Object are stored by calling put(key ,value) and retirieve by get(key)

When we call put(k,v) machCode() is called and calulates the index of the bucket location. when we call get method with key object it will lands to the same index or bucket and retrive the value.
IF there are multiple keys which give same bucket location with the hashcode(), then the linked list is used to store thos keys in the bucket location and new entry is store in the next node. when get method is called then it will locate to the bucket location with the given key and to get the exact value , equals() method. This will cehck each node in the linked list and when it returns true that value is return from the linked list.

Comments