Code Examples & DSA Practice

Explore classic algorithms, LeetCode problems, and 100 days of DSA challenges. Practice dry-running code with detailed step-by-step analysis.

🐍

Fibonacci Sequence

Algorithms

Recursive implementation of the Fibonacci sequence. Great for understanding recursion and function calls.

BeginnerRecursionBase CaseFunction Calls
def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

# Test the function
result = fibonacci(5)
print(f"Fibonacci of 5 is: {result}")
🐍

Bubble Sort Algorithm

Sorting

Classic sorting algorithm that demonstrates nested loops and array manipulation.

IntermediateNested LoopsArray ManipulationSwapping
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

# Test the function
numbers = [64, 34, 25, 12, 22, 11, 90]
sorted_numbers = bubble_sort(numbers.copy())
print(f"Original: {numbers}")
print(f"Sorted: {sorted_numbers}")
🔧

Binary Search

Searching

Efficient search algorithm for sorted arrays. Demonstrates divide and conquer.

IntermediateDivide and ConquerArray IndexingConditional Logic
#include <stdio.h>

int binarySearch(int arr[], int size, int target) {
    int left = 0;
    int right = size - 1;
    
    while (left <= right) {
        int mid = left + (right - left) / 2;
        
        if (arr[mid] == target) {
            return mid;
        }
        
        if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    
    return -1; // Element not found
}

int main() {
    int arr[] = {2, 3, 4, 10, 40};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target = 10;
    
    int result = binarySearch(arr, size, target);
    
    if (result != -1) {
        printf("Element found at index %d\n", result);
    } else {
        printf("Element not found\n");
    }
    
    return 0;
}
🐍

Two Sum (LeetCode #1)

LeetCode

Find two numbers in an array that add up to a specific target. Classic hash map problem.

BeginnerHash MapArrayTwo Pointers
def two_sum(nums, target):
    # Hash map to store number and its index
    num_map = {}
    
    for i, num in enumerate(nums):
        complement = target - num
        
        # Check if complement exists in hash map
        if complement in num_map:
            return [num_map[complement], i]
        
        # Store current number and its index
        num_map[num] = i
    
    return []  # No solution found

# Test the function
nums = [2, 7, 11, 15]
target = 9
result = two_sum(nums, target)
print(f"Indices: {result}")
print(f"Numbers: {nums[result[0]]}, {nums[result[1]]}")
🐍

Maximum Subarray (LeetCode #53)

LeetCode

Find the contiguous subarray with the largest sum using Kadane's algorithm.

IntermediateDynamic ProgrammingKadane's AlgorithmArray
def max_subarray(nums):
    # Kadane's Algorithm
    max_sum = nums[0]
    current_sum = nums[0]
    
    for i in range(1, len(nums)):
        # Either extend the existing subarray or start new one
        current_sum = max(nums[i], current_sum + nums[i])
        
        # Update maximum sum found so far
        max_sum = max(max_sum, current_sum)
    
    return max_sum

# Test with an array
test_array = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
result = max_subarray(test_array)
print(f"Maximum subarray sum: {result}")

Linked List Operations

Data Structures

Basic linked list implementation with insertion and traversal.

AdvancedPointersDynamic MemoryData Structures
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
    
    Node(int value) : data(value), next(nullptr) {}
};

class LinkedList {
private:
    Node* head;
    
public:
    LinkedList() : head(nullptr) {}
    
    void insert(int value) {
        Node* newNode = new Node(value);
        newNode->next = head;
        head = newNode;
    }
    
    void display() {
        Node* current = head;
        while (current != nullptr) {
            cout << current->data << " -> ";
            current = current->next;
        }
        cout << "NULL" << endl;
    }
};

int main() {
    LinkedList list;
    list.insert(3);
    list.insert(2);
    list.insert(1);
    
    cout << "Linked List: ";
    list.display();
    
    return 0;
}

Ready to Analyze Your Own Code?

Use our AI-powered code editor to get step-by-step analysis of any code you write.

Start Coding