Category: DSA

Posted on: March 12, 2025 Posted by: rahulgite Comments: 0

Comprehensive Guide to Tree Algorithms

1. Tree Basics A tree is a hierarchical data structure consisting of nodes, where each node has a parent (except the root) and zero or more children. Trees are a type of graph with no cycles and a single connected component. Types of Trees: Tree Terminology: 2. Tree Traversal Algorithms 2.1 Inorder Traversal (Left, Root, Right) Description: Visits nodes in ascending order for a BST. Example Tree: Traversal Order: 1…

Posted on: March 12, 2025 Posted by: rahulgite Comments: 0

Comprehensive Guide to Graph Algorithms

1. Graph Basics A graph is a collection of vertices (nodes) connected by edges (links). Graphs can be: Graph Representation: 2. Depth-First Search (DFS) Description: DFS explores as far as possible along each branch before backtracking. It can be implemented using recursion or a stack. Steps: Given Graph: Starting from Node 1: 1 → 2 → 4 → 5 → 3 Time Complexity: Space Complexity: Applications: 3. Breadth-First Search (BFS)…

Posted on: March 12, 2025 Posted by: rahulgite Comments: 0

Comprehensive Guide to Searching Algorithms

1. Linear Search Description: Linear Search is the simplest searching algorithm. It sequentially checks each element in the list until the target value is found or the end of the list is reached. Steps: Given: [5, 3, 8, 4, 2], Target: 4 Time Complexity: Space Complexity: 2. Binary Search Description: Binary Search works on sorted arrays. It repeatedly divides the search interval in half until the target value is found.…

Posted on: March 12, 2025 Posted by: rahulgite Comments: 0

Comprehensive Guide to Sorting Algorithms

1. Bubble Sort Description: Bubble Sort is a simple comparison-based algorithm where adjacent elements are compared and swapped if they are in the wrong order. This process repeats until the entire array is sorted. Steps: Given: [5, 3, 8, 4, 2] Repeat until the list is sorted: [2, 3, 4, 5, 8] Time Complexity: Space Complexity: 2. Selection Sort Description: Selection Sort selects the smallest element from an unsorted portion…

Posted on: March 12, 2025 Posted by: rahulgite Comments: 0

Approaches for finding pairs with a sum of 5

Finding Pairs with a Given Sum using HashSet in Java 1. Approach Overview Given an array of integers, the goal is to find all unique pairs where the sum equals 5. We will use a HashSet to efficiently track the elements and their complements. 2. HashSet Approach for Unsorted Input Algorithm Java Code Output Time Complexity Space Complexity 3. Optimized Approach for Sorted Input If the array is already sorted,…

Posted on: February 1, 2025 Posted by: rahulgite Comments: 0

Stack & Queue

Problem 1: Implement Stack using Queues Explanation: Implement a stack using only queues. Approach: Time & Space Complexity: Java Implementation: Example: Input: Output: Edge Cases Considered: Problem 2: Design Hit Counter Explanation: Design a hit counter that counts the number of hits received in the past 300 seconds. Approach: Time & Space Complexity: Java Implementation: Example: Input: Output: Edge Cases Considered:

Posted on: February 1, 2025 Posted by: rahulgite Comments: 0

Dynamic Programming

Problem 1: Climbing Stairs Explanation: You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Approach: Time & Space Complexity: Java Implementation: Example: Input: Output: Edge Cases Considered: Problem 2: House Robber Explanation: Given an integer array nums representing the amount of money in houses, return…

Posted on: February 1, 2025 Posted by: rahulgite Comments: 0

Tree & Graph Problems

Problem 1: Maximum Depth of Binary Tree Explanation: Given a binary tree, return its maximum depth. Approach: Time & Space Complexity: Java Implementation: Example: Input: Output: Edge Cases Considered: Problem 2: Validate Binary Search Tree Explanation: Given a binary tree, determine if it is a valid Binary Search Tree (BST). Approach: Time & Space Complexity: Java Implementation: Example: Input: Output: Edge Cases Considered: Problem 3: Count Complete Tree Nodes Explanation:…

Posted on: February 1, 2025 Posted by: rahulgite Comments: 0

String Problems

Problem 1: Longest Common Prefix Explanation: Given an array of strings, find the longest common prefix among them. If no common prefix exists, return an empty string. Approach: Time & Space Complexity: Java Implementation: Example: Input: Output: Edge Cases Considered: Problem 2: Valid Anagram Explanation: Given two strings s and t, return true if t is an anagram of s, and false otherwise. Approach: Time & Space Complexity: Java Implementation:…

Posted on: February 1, 2025 Posted by: rahulgite Comments: 0

Linked List problems

Problem 1: Linked List Cycle Explanation: Given a linked list, determine if it has a cycle in it. Approach: Time & Space Complexity: Java Implementation: Example: Input: Output: Edge Cases Considered: Problem 2: Intersection of Two Linked Lists Explanation: Given the heads of two singly linked lists, return the node where the two lists intersect, or null if they do not intersect. Approach: Time & Space Complexity: Java Implementation: Example:…