Most Used Problem Solving Patterns
Most Used Problem Solving Patterns
Problem Solving
Patterns
50
A
25 30
B C
40 10 30 60
D E F G
Interviews?
questions to them
1
Two Pointers
Y Two pointers traverse the data structure together until a
specific condition is met.^
Y Handy for finding pairs in sorted arrays or linked lists, such
as comparing elements to each other in an arrayk
Y Can be of different types - both pointers starting from
same end, or one pointer at each end.
Example Problem
2
We can use two pointers approach for an optimised solutio
A forward
Pointer 1 Pointer 2
1 2 3 5 6 9
Pointer 1 Pointer 2
1 2 3 5 6 9
Pointer 1 Pointer 2
1 2 3 5 6 9
3
The optimised solution using Two pointers would follow the
pseudocode
sort(nums)
i = 0
j = length of nums - 1
while i < j:
return i, j
i++
else:
j--
return 0
4
Problems based on Two
Pointers:
% Remove Duplicates from Sorted Array -
LeetCod
% Squares of a Sorted Array - LeetCod
% 3Sum - LeetCod
% 3Sum Closest - LeetCod
% Sort Colors - LeetCod
% Backspace String Compare - LeetCode
5
Fast and Slow Pointers
Example Problem
6
To solve this problem in a single traversal we can use the
Hare and Tortoise Approach
There are two pointers used in this method, slow and fast
When the fast pointer reaches the end of the linked list, the
slow pointer will have reached the middle of the linked list.
head
7 3 5 17 21 25 81
7 3 5 17 21 25 81
7 3 5 17 21 25 81
slow fast
head
7 3 5 17 21 25 81
slow fast
head
7 3 5 17 21 25 81
Middle Node
7
Pseudocode for Fast and
Slow Pointer Approach
slowPointer = head
fastPointer = head
slowPointer = slowPointer.next
fastPointer = fastPointer.next.next
middleElement = slowPointer.data
8
Problems based on Slow and
Fast Pointer:
Linked List Cycle - LeetCod
Happy Number - LeetCod
Palindrome Linked List - LeetCod
Reorder List - LeetCod
Circular Array Loop - LeetCode
9
Sliding Window
Used to perform a required operation on a specific window
size of a given array or linked lis
Useful for problems dealing with subarrays or sublists
Example Problem
10
We can improve this approach by using the sliding window
technique to move the window over the array from index K to
the end of the array
k=3
1 2 6 2 4 1 window-sum=9
max-sum=9
1 2 6 2 4 1 window-sum=10
max-sum=10
1 2 6 2 4 1 window-sum=12
max-sum=12
1 2 6 2 4 1 window-sum=7
max-sum=12
11
Pseudocode for the Sliding
Window approach
function maxSumSubarrayOfSizeK(arr, K):
// Initialize variables
maxSum = 0
currentSum = 0
for i from 0 to K - 1:
maxSum = currentSum
// Slide the window and update maxSum until the end of the
array is reached
return maxSum
12
Problems based on Sliding
Window Approach:
Characters - LeetCod
Characters - LeetCod
13
Merged Intervals
This technique is used to deal with problems that require
you to find overlapping intervals.
Example Problem
Time
14
The brute force approach would involve starting from the
other interval from the list and merge the other into the
first interva
number of intervals
0 1 2 3 4 5 6 7 8 9 10
(1, 4) (7, 9)
(3, 6)
(8, 10)
Overlapping Intervals
Output:
15
The optimised version to solve the problem
interval%
the stack then, push the current interval into the stack%
& If the current interval does overlap with the top of the
stack then, update the stack top with the ending time
16
Pseudocode for the optimised
approach
if n <= 0:
return
stack s
sort arr
s.push(arr[0])
top = s.top()
s.push(arr[i])
top.end = arr[i].end
s.pop()
s.push(top)
return s
17
Problems based on Merge
Intervals Approach:
18
Depth First Search (DFS)
Example Problem
Given a binary tree and a number S, find if the tree has a path
from root-to-leaf such that the sum of all the node values of
that path equals S.
To solve this problem we can start from the root and at every
step, make two recursive calls one for the left and one for the
right child.
50
25 30
B C
40 10 30 60
D E F G
19
DFS Approach
9+ Start from the root of the tree and traverse the tree in a
depth-first manner!
+ If the current node is NOT a leaf node, do
Subtract the value of the current node from the given
target sum to get updated sum value → S = S -
node.valu&
Make two recursive calls for left and right children of
the current node with updated sum value-
+ At every step, check if the current node is a leaf node and
if its value is equal to target sum S. If both conditions are
met, we have found the required root-to-leaf path,
therefore return true!
If the current node is a leaf but its value is not equal to the
given number S, return false.
20
Pseudocode for DFS
Approach
function hasPath(root, sum):
return false
return true
21
Problems based on DFS
Approach:
$ Path Sum II - LeetCod!
22
Breadth First Search (BFS)
' Starts from a chosen source node, visits all its neighbors,
then moves on to their neighbors, and so on, until all nodes
are visited or a specific condition is met.$
Example Problem
1 Level 1
2 3 Level 2
4 5 6 9 Level 3
7 8 Level 4
⁘ LEVEL ORDER: 1, 2, 3, 4, 5, 6, 9, 7, 8
23
BFS Based Approach:
level
If the queue is not empty, repeat from step 3 for the next level.
24
BFS Based Approach:
Level 1 3
Level 2 9 20
Level 3 15 7
Queue
Since the queue is not empty 3 is popped out and added to the
Queue
9 20
traversal order
Queue
20
25
Now 20 is popped out and added to the final traversal order.
15 7
15 and 7 are popped out with the same process and we obtain
final level order traversal
26
Problems based on BFS
Approach:
' Binary Tree Level Order Traversal II -
LeetCod
' Binary Tree Zigzag Level Order Traversal -
LeetCod
' Average of Levels in Binary Tree - LeetCod
' Maximum Level Sum of a Binary Tree -
LeetCod
-' Minimum Depth of Binary Tree - LeetCod
1' Maximum Depth of Binary Tree - LeetCod
"' Populating Next Right Pointers in Each
Node - LeetCod
' Binary Tree Right Side View - LeetCode
27
Why
Bosscoder?
750+ Alumni placed at Top
Product-based companies.
Explore More