0% found this document useful (0 votes)
5 views1 page

DSA Python Pattern Notes

The document outlines various data structures and algorithms including Hashing with dictionaries for constant time lookups, Sliding Window for subarray problems, Two Pointers for sorted arrays, Stack for Last-In-First-Out operations, Heap for Top K problems, and BFS/DFS for tree and graph traversal. Each section provides a brief description and example usage. These techniques are essential for solving common programming challenges.

Uploaded by

Shreyas GN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

DSA Python Pattern Notes

The document outlines various data structures and algorithms including Hashing with dictionaries for constant time lookups, Sliding Window for subarray problems, Two Pointers for sorted arrays, Stack for Last-In-First-Out operations, Heap for Top K problems, and BFS/DFS for tree and graph traversal. Each section provides a brief description and example usage. These techniques are essential for solving common programming challenges.

Uploaded by

Shreyas GN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Hashing (Dictionary)

Hashing allows constant time lookup using key-value pairs. In Python, dictionaries are used.
Common use cases include counting frequency, checking duplicates, and solving pair problems like
Two Sum. Example: d[x] = [Link](x,0) + 1. This avoids key errors and updates counts efficiently.

2. Sliding Window

Sliding window is used for subarray or substring problems. Maintain a window using two pointers.
Expand the window and shrink when conditions break. Example pattern: while condition: left += 1.
Useful for longest substring problems.

3. Two Pointers

Used when array is sorted or when scanning from both ends. Move left and right pointers based on
condition. Example: if sum < target, move left; else move right.

4. Stack

Stack follows Last-In-First-Out. Implement using Python list. Used for problems like valid
parentheses and monotonic stack problems. Operations: append() and pop().

5. Heap (Priority Queue)

Heaps are used for Top K problems. Python provides heapq which is a min heap. For max heap,
push negative values. Operations: heappush and heappop.

6. BFS and DFS

BFS uses queue (deque) and explores level by level. DFS uses recursion or stack. Used in trees
and graphs like Number of Islands.

You might also like