0% found this document useful (0 votes)
11 views9 pages

Famous Algorithms: Sorting Algorithms

The document outlines important coding and data structure algorithms commonly asked in interviews, including sorting, searching, graph, dynamic programming, greedy, and divide and conquer algorithms. It provides example solutions for various coding problems such as reversing an array, finding duplicates, reversing a linked list, implementing a stack and queue, and constructing a binary search tree. Additionally, it includes methods for calculating the height of a tree and implementing a graph using an adjacency list.

Uploaded by

prajakta.ghugare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Famous Algorithms: Sorting Algorithms

The document outlines important coding and data structure algorithms commonly asked in interviews, including sorting, searching, graph, dynamic programming, greedy, and divide and conquer algorithms. It provides example solutions for various coding problems such as reversing an array, finding duplicates, reversing a linked list, implementing a stack and queue, and constructing a binary search tree. Additionally, it includes methods for calculating the height of a tree and implementing a graph using an adjacency list.

Uploaded by

prajakta.ghugare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

IMPORTANT CODING DSA QUESTIONS THAT ARE MOSTLY

ASKED:
Famous Algorithms:
1. Sorting Algorithms:
 Bubble Sort
 Selection Sort
 Insertion Sort
 Merge Sort
 Quick Sort
 Heap Sort
2. Searching Algorithms:
 Linear Search
 Binary Search
3. Graph Algorithms:
 Breadth-First Search (BFS)
 Depth-First Search (DFS)
 Dijkstra's Algorithm
 Bellman-Ford Algorithm
 Floyd-Warshall Algorithm
4. Dynamic Programming Algorithms:
 Fibonacci Series
 Longest Common Subsequence
 Shortest Path Problems
5. Greedy Algorithms:
 Huffman Coding
 Activity Selection Problem
6. Divide and Conquer Algorithms:
 Merge Sort
 Quick Sort
 Binary Search

Reverse Array:
// Example solution
public class ReverseArray {
public static void reverse(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5};
reverse(arr);
System.out.println(Arrays.toString(arr)); // [5, 4, 3, 2, 1]
}
}
Find Duplicate:
// Example solution
public class FindDuplicate {
public static int findDuplicate(int[] arr) {
Set<Integer> set = new HashSet<>();
for (int num : arr) {
if (!set.add(num)) {
return num;
}
}
return -1; // no duplicate found
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5, 2};
int duplicate = findDuplicate(arr);
System.out.println(duplicate); // 2
}
}
Reverse Linked List
// Example solution
public class ReverseLinkedList {
public static Node reverse(Node head) {
Node prev = null;
Node current = head;
while (current != null) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}

public static void main(String[] args) {


Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head = reverse(head);
System.out.println(head.data); // 3
}
}

class Node {
int data;
Node next;

public Node(int data) {


this.data = data;
}
}
Implement Stack using Array:
// Example solution
public class ArrayStack {
private int[] arr;
private int top;

public ArrayStack(int capacity) {


arr = new int[capacity];
top = -1;
}

public void push(int element) {


if (top == arr.length - 1) {
throw new RuntimeException("Stack is full");
}
arr[++top] = element;
}

public int pop() {


if (top == -1) {
throw new RuntimeException("Stack is empty");
}
return arr[top--];
}

public static void main(String[] args) {


ArrayStack stack = new ArrayStack(5);
stack.push(1);
stack.push(2);
System.out.println(stack.pop()); // 2
}
}
Implement Queue using Linked List:
// Example solution
public class LinkedListQueue {
private Node head;
private Node tail;
public void enqueue(int element) {
Node node = new Node(element);
if (head == null) {
head = node;
tail = node;
} else {
tail.next = node;
tail = node;
}
}

public int dequeue() {


if (head == null) {
throw new RuntimeException("Queue is empty");
}
int element = head.data;
head = head.next;
return element;
}

public static void main(String[] args) {


LinkedListQueue queue = new LinkedListQueue();
queue
Implement Binary Search Tree (BST)
// Example solution
public class BinarySearchTree {
private Node root;

public void insert(int key) {


root = insert(root, key);
}

private Node insert(Node node, int key) {


if (node == null) {
return new Node(key);
}
if (key < node.key) {
node.left = insert(node.left, key);
} else if (key > node.key) {
node.right = insert(node.right, key);
}
return node;
}

public void inorder() {


inorder(root);
}

private void inorder(Node node) {


if (node != null) {
inorder(node.left);
System.out.print(node.key + " ");
inorder(node.right);
}
}

public static void main(String[] args) {


BinarySearchTree bst = new BinarySearchTree();
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(2);
bst.insert(4);
bst.insert(6);
bst.insert(8);
bst.inorder(); // 2 3 4 5 6 7 8
}
}
class Node {
int key;
Node left;
Node right;

public Node(int key) {


this.key = key;
}
}
Find Height of a Tree:
// Example solution
public class TreeHeight {
public static int height(Node node) {
if (node == null) {
return 0;
}
return 1 + Math.max(height(node.left),
height(node.right));
}

public static void main(String[] args) {


Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
System.out.println(height(root)); // 3
}
}

class Node {
int key;
Node left;
Node right;
public Node(int key) {
this.key = key;
}
}
Implement Graph using Adjacency List:
// Example solution
public class Graph {
private Map<Integer, List<Integer>> adjacencyList;

public Graph() {
adjacencyList = new HashMap<>();
}

public void addEdge(int source, int destination) {


adjacencyList.computeIfAbsent(source, k -> new
ArrayList<>()).add(destination);
}

public void printGraph() {


for (Map.Entry<Integer, List<Integer>> entry :
adjacencyList.entrySet()) {
System.out.println(entry.getKey() + " -> " +
entry.getValue());
}
}

public static void main(String[] args) {


Graph graph = new Graph();
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);
graph.printGraph();
// 0 -> [1, 2]
// 1 -> [3]
// 2 -> [4]
}
}

You might also like