Skip to content

dipanjannC/DSA4Interviews

Repository files navigation

DSA with Python

A comprehensive collection of Data Structures and Algorithms implemented in Python, designed for learning, interview preparation, and competitive programming.

📚 Table of Contents

🎯 Overview

This repository contains clean, well-documented implementations of fundamental data structures and algorithms in Python. Each implementation includes:

  • Clear explanations of concepts
  • Time and space complexity analysis
  • Visual representations where applicable
  • Real-world use cases and examples
  • Test cases and sample inputs/outputs

Perfect for:

  • 📖 Learning DSA concepts
  • 💼 Technical interview preparation
  • 🏆 Competitive programming practice
  • 🎓 Computer science students
  • 👨‍💻 Software engineering interviews

📁 Repository Structure

DSA-with-Python/
├── Algorithms/
│   ├── fundamentals/
│   │   ├── heap/
│   │   │   └── min_heap_with_lib.py
│   │   ├── sorting/
│   │   ├── searching/
│   │   └── graph/
│   ├── dynamic_programming/
│   ├── greedy/
│   └── divide_conquer/
├── Data_Structures/
│   ├── linear/
│   │   ├── arrays/
│   │   ├── linked_lists/
│   │   ├── stacks/
│   │   └── queues/
│   ├── trees/
│   │   ├── binary_tree/
│   │   ├── bst/
│   │   └── avl_tree/
│   ├── graphs/
│   └── heaps/
├── Problems/
│   ├── leetcode/
│   ├── hackerrank/
│   └── practice/
├── ADVANCED_DATA_STRUCTURES.md
└── README.md

🏗️ Data Structures

Linear Data Structures

  • Arrays & Lists - Dynamic arrays, operations, and applications
  • Linked Lists - Singly, doubly, and circular linked lists
  • Stacks - LIFO operations, expression evaluation
  • Queues - FIFO operations, BFS implementation
  • Deques - Double-ended queues

Non-Linear Data Structures

  • Binary Trees - Tree traversals, operations
  • Binary Search Trees - Search, insert, delete operations
  • AVL Trees - Self-balancing binary search trees
  • Heaps - Min/Max heaps, priority queues
  • Graphs - Adjacency list/matrix, traversals
  • Tries - Prefix trees for string operations

Advanced Structures

  • Segment Trees - Range queries and updates
  • Fenwick Trees - Binary indexed trees
  • Disjoint Set Union - Union-find data structure

🧮 Algorithms

Sorting Algorithms

  • Comparison-based: Quick Sort, Merge Sort, Heap Sort
  • Non-comparison: Counting Sort, Radix Sort, Bucket Sort
  • Hybrid: Tim Sort, Intro Sort

Searching Algorithms

  • Linear Search - Sequential search
  • Binary Search - Divide and conquer search
  • Interpolation Search - Improved binary search
  • Exponential Search - Unbounded search

Graph Algorithms

  • Traversals: DFS, BFS
  • Shortest Path: Dijkstra, Bellman-Ford, Floyd-Warshall
  • Minimum Spanning Tree: Kruskal, Prim
  • Topological Sort: DFS-based, Kahn's algorithm

Dynamic Programming

  • Classic Problems: Fibonacci, Knapsack, LCS, LIS
  • Advanced: Matrix Chain Multiplication, Edit Distance
  • Optimization: Memoization vs Tabulation

String Algorithms

  • Pattern Matching: KMP, Rabin-Karp, Boyer-Moore
  • String Processing: Manacher's, Z-algorithm

🚀 Getting Started

Prerequisites

  • Python 3.7 or higher
  • Basic understanding of Python programming
  • Familiarity with object-oriented programming concepts

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/DSA-with-Python.git
    cd DSA-with-Python
  2. Set up virtual environment (optional but recommended)

    python -m venv dsa_env
    source dsa_env/bin/activate  # On Windows: dsa_env\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt  # If requirements file exists

Running Examples

# Run a specific data structure implementation
python Algorithms/fundamentals/heap/min_heap_with_lib.py

# Run sorting algorithms
python Algorithms/sorting/quick_sort.py

# Run graph algorithms
python Algorithms/graph/dijkstra.py

💡 Usage Examples

Min Heap Implementation

from Algorithms.fundamentals.heap.min_heap_with_lib import MinHeap

# Create a min heap
heap = MinHeap()

# Add elements
numbers = [5, 2, 8, 1, 9, 3]
for num in numbers:
    heap.add(num)

print(heap)  # Min Heap: [1, 2, 3, 5, 9, 8]

# Extract minimum
min_val = heap.print_and_delete()
print(f"Minimum value: {min_val}")  # Minimum value: 1

Binary Search Tree

from Data_Structures.trees.bst import BST

# Create BST and insert values
bst = BST()
values = [50, 30, 70, 20, 40, 60, 80]

for val in values:
    bst.insert(val)

# Search for a value
result = bst.search(40)
print(f"Found 40: {result}")  # Found 40: True

Graph Traversal

from Algorithms.graph.dfs_bfs import Graph

# Create graph and add edges
graph = Graph()
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'D')

# Perform DFS
print("DFS traversal:")
graph.dfs('A')

🤝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/new-algorithm
  3. Add your implementation
    • Follow the existing code style
    • Include docstrings and comments
    • Add test cases
    • Update documentation if needed
  4. Commit your changes
    git commit -m "Add: Implement [Algorithm/Data Structure name]"
  5. Push to your branch
    git push origin feature/new-algorithm
  6. Create a Pull Request

Contribution Guidelines

  • Code Quality: Write clean, readable, and well-documented code
  • Testing: Include test cases for your implementations
  • Documentation: Add clear explanations and complexity analysis
  • Naming: Use descriptive variable and function names
  • Style: Follow PEP 8 Python style guidelines

📖 Resources

Books

  • "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
  • "Algorithms" by Robert Sedgewick and Kevin Wayne
  • "Data Structures and Algorithms in Python" by Goodrich, Tamassia, and Goldwasser

Online Platforms

Python Documentation

📊 Complexity Cheat Sheet

Data Structure Access Search Insertion Deletion
Array O(1) O(n) O(n) (shifting elements) O(n) (shifting elements)
Singly Linked List O(n) O(n) O(1)* (given a node or at head) O(1)* (given the node to delete)
Stack (LIFO) O(n) O(n) O(1) (push) O(1) (pop)
Queue (FIFO) O(n) O(n) O(1) (enqueue) O(1) (dequeue)
Binary Tree O(n) O(n) O(1)* (if you know where to insert) / O(n) to find spot O(1)* (if you know the node) / O(n) to find it
BST (average) O(log n) O(log n) O(log n) O(log n)
BST (worst-case) O(n) O(n) O(n) O(n)
Hash Table (avg.) O(1) O(1) O(1) O(1)
Hash Table (worst) O(n) O(n) O(n) O(n)
Heap (min or max) O(1) (peek root) O(n) (arbitrary) O(log n) O(log n) (remove root)

* Truly O(1) for a linked list or general binary tree only if you already have a direct reference to the node (or insertion point).

🏷️ Tags

python data-structures algorithms interview-prep competitive-programming computer-science dsa coding-interview leetcode programming

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🌟 Show Your Support

If this repository helped you, please give it a ⭐️!


Happy Coding! 🚀

Made with ❤️ for the programming community

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages