20 Coding Patterns-1
20 Coding Patterns-1
20 Coding Patterns-1
PATTERNS
TO MASTER IN TECHNICAL INTERVIEWS
+91-7260058093
info@algotutor.io
www.algotutor.io
1. Two Pointers
Applications:
It is used when we need to find pairs or sub-
arrays in an array that satisfy a certain
condition, or when we need to find a specific
element in a sorted array.
DSA Usages:
Array, String, Linked List
Pointer Pointer
01
Sample Problems:
Finding a pair of numbers that add up to a
target sum in a sorted or unsorted array.
Input: "babad"
Output: "bab"
02
2. Fast & Slow Pointers
Applications:
It is used in DSA to solve problems that
involve linked lists, arrays, or other data
structures where we need to iterate through
the data in a specific way.
03
Sample Problems:
Detecting cycles in a linked list.
04
3. Sliding Window
Applications:
It is used in scenarios where we need to find
a subarray or a substring that meets certain
criteria, such as having a specific sum or
product, or containing a certain pattern or
character and the input data in a specific
window size.
DSA Usages:
Array, String, Linked List
05
Sample Problems:
Finding the maximum or minimum sum
subarray of a given size.
Input: "abcabcbb"
Output: "abc"
DSA Usages:
Linked List
07
Sample Problems:
Reversing a linked list of any size.
Input: 1->2->3->4->5
Output: 5->4->3->2->1
Input: 1->2->3->2->1
Output: True
08
5. Modified Binary
Search
Applications:
This method is used when the conventional
binary search algorithm cannot be used to
solve the problem because the search
condition is more complex than simply
checking if an element is greater than or less
than the middle element.
DSA Usages:
Array
09
Sample Problems:
Finding the minimum or maximum
element in a rotated sorted array.
Input: [4, 5, 6, 7, 0, 1, 2]
Output: 0 (minimum) or 7 (maximum)
Advanced
Data Science MERN Stack
CONTACT US TO LEARN MORE
& Machine Development
Info@algotutor.io +91-7260058093 Learning
10
6. Islands
(Matrix Traversal)
Applications:
It is used to solve problems that involve
traversing a matrix or grid to find a specific
pattern or object. This method is used when
the problem requires identifying clusters or
groups of cells in the matrix that meet certain
criteria.
DSA Usages:
Matrix, Queue
11
Sample Problems:
Counting the number of islands in a
binary matrix.
12
7. Merge Intervals
Applications:
The Merge Interval is a popular method for
solving problems that involve merging or
overlapping intervals. It is used when the
problem requires combining or merging
intervals that share a common overlap or
intersection.
DSA Usages:
Array, Heap
13
Sample Problems:
Given a collection of intervals, merge all
overlapping intervals.
14
8. Two Heaps
Applications:
The Two Heaps is a popular method for
solving problems that involve managing and
processing elements in two separate heaps
simultaneously. It is used when the problem
requires maintaining two sets of elements
with specific ordering or prioritization
properties.
DSA Usages:
Heap, Array
15
Sample Problems:
Find the median of streaming data.
Input: [2, 5, 1, 3, 6, 4]
Output: [2, 3.5, 2, 2.5, 3, 3.5]
DSA Usages:
Heap, Array
17
Sample Problems:
Maximum distinct elements after
removing k elements.
Input: [3, 7, 1, 4, 5, 2] = 3
Output: 3
Input: [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
18
Why Choose AlgoTutor?
1:1 PERSONAL
100 % PLACEMENT
MENTORSHIP FROM
ASSISTANCE
INDUSTRY EXPERTS
100 % SUCCESS
23 LPA(AVG.)CTC
RATE
LEARN FROM
CAREER SERVICES
SCRATCH
EXPLORE MORE
DSA Usages:
Array, Queue, Heap
19
Sample Problems:
Merging K sorted arrays into a single
sorted array.
Input: [[1,5,9],[10,11,13],[12,13,15]], K = 8
Output: 13
20
11. Cyclic Sort
Applications:
The Cyclic Sort is a popular method for
solving problems that involve sorting an array
of numbers in a specific range. It is used when
the problem requires arranging the elements
of the array in a cyclic manner to achieve the
desired order.
DSA Usages:
Array
21
Sample Problems:
Sorting an array of distinct numbers.
Input: [3, 1, 5, 4, 2]
Output: [1, 2, 3, 4, 5]
Input: [3, 2, 0, 1]
Output: [0, 1, 2, 3]
22
12. Breadth-First
Search
Applications:
BFS is a popular method for solving problems
related to traversing or searching in a graph
or tree data structure. It is used when the
problem requires exploring or visiting all the
vertices or nodes of the graph in a breadth-
wise manner, i.e., exploring the vertices at the
same level before moving to the next level.
DSA Usages:
Tree, Graph, Matrix, Queue
23
Sample Problems:
Find the shortest path or distance
between two BST nodes.
Input: [4,2,6,1,3]
Output: 1
Input: [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Input: [3,9,20,null,null,15,7]
Output: 2
Input: [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
24
13. Depth-First
Search
Applications:
DFS is a popular method for solving problems
related to traversing or searching in a graph
or tree data structure. It is used when the
problem requires exploring or visiting all the
vertices or nodes of the graph in a depth-wise
manner, i.e., exploring as far as possible along
each branch before backtracking.
DSA Usages:
Tree, Graph, Matrix
25
Sample Problems:
Find the connected components in a
graph.
27
Sample Problems:
Given the total number of n tasks and a
list of prerequisite pairs of size m. Find
the order of tasks you should pick to
finish all tasks.
DSA Usages:
Array, HashTable
29
Sample Problems:
Calculate the nth Fibonacci number.
Input: n = 10
Output: 55
Input: n = 2
Output: 2
DSA Usages:
Queue, Array, String
31
Sample Problems:
Given a set of elements, generate all
possible subsets, including the empty set
and the set itself.
Input: [1, 2, 3]
Output: {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3},
{1, 2, 3}
DSA Usages:
Array, HashTable
33
Sample Problems:
Given a list of items with their weights and
values, and a knapsack with a weight
capacity, find the maximum value that can
be obtained by selecting a subset of items
to fit into the knapsack without exceeding
its weight capacity.
Input: [2, 3], [3, 4], [4, 5], [5, 8], [9, 10]]
Output: 17
34
18. Palindromic
Subsequence
Applications:
The Palindromic Subsequence method is a
technique used in DSA to solve problems that
involve identifying and manipulating
palindromic subsequences within a given
string or sequence. It is used when the
problem requires finding or manipulating
palindromic subsequences with specific
properties or constraints.
DSA Usages:
Array, HashTable
35
Sample Problems:
Longest Palindromic Subsequence.
Input: "BBABCBCAB"
Output: "BABCBAB" with a length of 7
Input: "ACBCDBAA"
Output: 19
Input: "ACBCDBAA"
Output: 19
Input: "ACBCDBAA"
Output: 19 36
19. Longest
Common Substring
Applications:
The Longest Common Substring method is
used in DSA to solve problems that involve
finding the longest common substring
between two or more strings. It is used when
the problem requires identifying the longest
continuous sequence of characters that is
common to multiple strings.
DSA Usages:
Array, HashTable
37
20. Bitwise XOR
Applications:
The Bitwise XOR (Exclusive OR) method is
used in DSA to solve various problems that
involve bitwise operations and manipulation
of binary representations of numbers. It is
used when the problem requires performing
operations or extracting information by
manipulating individual bits using XOR logic.
DSA Usages:
Array, Bits
38
Sample Problems:
Given an array of integers where every
element appears twice except for one
element, find that single element.
Input: [2, 4, 6, 2, 4]
Output: 6
Input: [ 2, 3, 7, 9, 11, 2, 3, 11 ]
Output: 7, 9
100 % SUCCESS
23 LPA(AVG.)CTC
RATE
LEARN FROM
CAREER SERVICES
SCRATCH
EXPLORE MORE
EXPLORE MORE