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 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

101010101010101010101010101010101010101010101

www.bosscoderacademy.com
Introduction
Online resources to practise DSA problems have
thousands of questions over a wide range of topics.

For any person, it may become overwhelming with


the large amount of problems to cover and the fear
of missing out on important questions is always
lurking.

Here, we have analysed, shortlisted and compiled a


set of 100 questions that will help you gain
complete confidence over data structures and
algorithms.

However as a disclaimer, you should know that


everyone learns differently and at the end of the
day, it is not the number of questions that you solve
but gaining the ability to analyse, identify and come
up with solutions for related problems is what
matters.

ARRAYS
Question 1.
Write a program to reverse an array

Practice Here

Question 2.
Find the kth smallest element in an array in linear
time complexity.

Practice Here

Question 3.
Given an integer array arr, find the contiguous
subarray (containing at least one number) which
has the largest sum and return its sum and print
the subarray.

Practice Here
Question 4.

You are given an array of prices where prices[i] is the

price of a given stock on an ith day. You want to

maximise your profit by choosing a single day to

buy one stock and choosing a different day in the

future to sell that stock. Return the maximum profit

you can achieve from this transaction. If you cannot

achieve any profit, return 0.

Practice Here

Question 5.

Given an array consisting of only 0s, 1s and 2s. Write

a program to in-place sort the array without using

inbuilt sort functions, in linear time complexity and

constant space.

Practice Here
Question 6.

Given an unsorted array of size n. Array elements are

in the range of 1 to n. One number from set {1, 2, …n}

is missing and one number occurs twice in the

array. Find these two numbers.

Practice Here

Question 7.

Given an array of integers and an integer target,

return indices of the two numbers such that they

add up to target.

Practice Here
Question 8.

You are given an integer array height of length n.

There are n vertical lines drawn such that the two

endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a

container, such that the container contains the

most water. Return the maximum amount of water

a container can store.

Practice Here
STRINGS
Question 1.
Write a function that reverses a string. The input
string is given as an array of characters s. You must
do this by modifying the input array in-place with
O(1) extra memory.
Practice Here

Question 2.
Given a string str of length N, you have to find the
number of palindromic subsequences (need not
necessarily be distinct) present in the string str.
Practice Here
Question 3.

Given two strings s and p, return an array of all the

start indices of p's anagrams in s.

Practice Here

Question 4.

Given a string s1 and a string s2, write a function to

check whether s2 is a rotation of s1.

Practice Here

Question 5.

You are given a string s and an integer k. You can

choose any character of the string and change it to

any other uppercase English character. You can

perform this operation at most k times.

Practice Here
Question 6.

Given two strings s and t of lengths m and n

respectively, return the minimum window substring

of s such that every character in t (including

duplicates) is included in the window. If there is no

such substring, return the empty string "".

Practice Here
SEARCHING AND SORTING
ALGORITHMS
Question 1.
Given an array of integers nums which is sorted in
ascending order, and an integer target, write a
function to search target in nums. If target exists,
then return its index. Otherwise, return -1. You must
write an algorithm with O(log n) runtime
complexity.
Practice Here

Question 2.
Given two sorted arrays nums1 and nums2 of size m
and n respectively, return the median of the two
sorted arrays. The overall run time complexity
should be O(log (m+n)).
Practice Here
Question 3.

Given an n x n matrix where each of the rows and

columns is sorted in ascending order, return the kth

smallest element in the matrix. Note that it is the

kth smallest element in the sorted order, not the

kth distinct element.

You must find a solution with a memory complexity

better than O(n2).

Practice Here

Question 4.

Given an integer array nums and an integer k,

return the kth largest element in the array.

Note that it is the kth largest element in the sorted

order, not the kth distinct element.

You must solve it in O(n) time complexity.

Practice Here
RECURSION

Question 1.

Given n pairs of parentheses, write a function to

generate all combinations of well-formed

parentheses.

Practice Here

Question 2.

Given an integer array nums of unique elements,

return all possible

Subsets (the power set).

The solution set must not contain duplicate subsets.

Return the solution in any order

Practice Here
Question 3.

Given an array nums of distinct integers, return all

the possible permutations. You can return the

answer in any order.

Practice Here

Question 4.

Given a string s, partition s such that every

substring of the partition is a palindrome

Return all possible palindrome partitioning of s.

Practice Here
Question 5.
Given a collection of candidate numbers
(candidates) and a target number (target), find all
unique combinations in candidates where the
candidate numbers sum to target.

Each number in candidates may only be used once


in the combination.

Note: The solution set must not contain duplicate


combinations.

Practice Here
HASHING
Question 1.
Given an array of integers nums and an integer
target, return indices of the two numbers such that
they add up to target.

You may assume that each input would have


exactly one solution, and you may not use the same
element twice.

You can return the answer in any order.

Practice Here

Question 2.
Given an unsorted integer array nums, return the
smallest missing positive integer.

You must implement an algorithm that runs in O(n)


time and uses constant extra space.

Practice Here
Question 3.

Design a data structure that follows the constraints

of a Least Recently Used (LRU) cache.

Practice Here

Question 4.

Given an array of strings, group the anagrams

together. You can return the answer in any order.

An Anagram is a word or phrase formed by

rearranging the letters of a different word or phrase,

typically using all the original letters exactly once.

Practice Here
MATRICES AND
MULTIDIMENSIONAL ARRAYS
Question 1.
Given an m x n matrix, return all elements of the
matrix in spiral order.
Practice Here

Question 2.
Given an m x n integer matrix matrix, if an element
is 0, set its entire row and column to 0's.

You must do it in place.


Practice Here
Question 3.

Determine if a 9 x 9 Sudoku board is valid. Only the

filled cells need to be validated according to the

following rules:

Each row must contain the digits 1-9 without

repetition.

Each column must contain the digits 1-9 without

repetition.

Each of the nine 3 x 3 sub-boxes of the grid must

contain the digits 1-9 without repetition.

Practice Here

Question 4.

You are given an n x n 2D matrix representing an

image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means

you have to modify the input 2D matrix directly. DO

NOT allocate another 2D matrix and do the rotation.

Practice Here
Question 5.
Write an efficient algorithm that searches for a
value target in an m x n integer matrix matrix. This
matrix has the following properties:

Integers in each row are sorted from left to right.

The first integer of each row is greater than the last


integer of the previous row.

Practice Here
LINKED LIST
Question 1.
Given the head of a singly linked list, reverse the list,
and return the reversed list.

Practice Here

Question 2.
Given head, the head of a linked list, determine if
the linked list has a cycle in it.

There is a cycle in a linked list if there is some node


in the list that can be reached again by
continuously following the next pointer. Internally,
pos is used to denote the index of the node that
tail's next pointer is connected to. Note that pos is
not passed as a parameter.

Return true if there is a cycle in the linked list.


Otherwise, return false.

Practice Here
Question 3.

You are given the heads of two sorted linked lists

list1 and list2.

Merge the two lists in a one sorted list. The list

should be made by splicing together the nodes of

the first two lists.

Return the head of the merged linked list.

Practice Here

Question 4.

Given the head of a linked list, remove the nth node

from the end of the list and return its head.

Practice Here
Question 5.
You are given the head of a singly linked-list. The list
can be represented as:

L0 → L1 → … → Ln - 1 → Ln

Reorder the list to be on the following form:

L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …

You may not modify the values in the list's nodes.


Only nodes themselves may be changed.

Practice Here

Question 6.
Given elements as nodes of the two linked lists. The
task is to multiply these two linked lists, say L1 and
L2.

Practice Here
Question 7.

Given the head node of a singly linked list return a

pointer to the middle of the linked list.

Practice Here

Question 8.

Given the heads of two singly linked-lists headA and

headB, return the node at which the two lists

intersect. If the two linked lists have no intersection

at all, return null.

Practice Here

Question 9.

Given the head of a singly linked list, return true if it

is a palindrome or false otherwise.

Practice Here
Question 10.
Given the head of a linked list, reverse the nodes of
the list k at a time, and return the modified list. k is
a positive integer and is less than or equal to the
length of the linked list. If the number of nodes is
not a multiple of k then left-out nodes, in the end,
should remain as it is.

You may not alter the values in the list's nodes, only
nodes themselves may be changed.

Practice Here
BIT MANIPULATION & MATH
CONCEPTS
Question 1.
Given an array nums containing n distinct numbers
in the range [0, n], return the only number in the
range that is missing from the array.

Practice Here

Question 2.
Given an integer n, return an array ans of length n +
1 such that for each i (0 <= i <= n), ans[i] is the
number of 1's in the binary representation of i.

Practice Here
Question 3.

Given a non-empty array of integers nums, every

element appears twice except for one. Find that

single one.

You must implement a solution with a linear

runtime complexity and use only constant extra

space.

Practice Here

Question 4.

Given a positive integer N, print count of set bits in

it.

Practice Here
Question 5.
Given two integer arrays nums1 and nums2, return
an array of their intersection. Each element in the
result must appear as many times as it shows in
both arrays and you may return the result in any
order.

Practice Here
STACKS AND QUEUES
Question 1.
Implement a first in first out (FIFO) queue using
only two stacks. The implemented queue should
support all the functions of a normal queue (push,
peek, pop, and empty).
Practice Here

Question 2.
Implement a last-in-first-out (LIFO) stack using only
two queues. The implemented stack should support
all the functions of a normal stack (push, top, pop,
and empty).
Practice Here
Question 3.

Given an array of integers temperatures represents

the daily temperatures, return an array answer such

that answer[i] is the number of days you have to

wait after the ith day to get a warmer temperature.

If there is no future day for which this is possible,

keep answer[i] == 0 instead.

Practice Here

Question 4.

Given an array of integers heights representing the

histogram's bar height where the width of each bar

is 1, return the area of the largest rectangle in the

histogram.

Practice Here
Question 5.
Given n non-negative integers representing an
elevation map where the width of each bar is 1,
compute how much water it can trap after raining.

Practice Here

Question 6.
Given a string s containing just the characters '(', ')',
'{', '}', '[' and ']', determine if the input string is valid.

Practice Here

Question 7.
Given a Queue Q containing N elements. The task is
to reverse the Queue. Your task is to complete the
function rev(), that reverses the N elements of the
queue.

Practice Here
Question 8.

Given a circular integer array A, return the next

greater element for every element in A. The next

greater element for an element x is the first

element greater than x that we come across while

traversing the array in a clockwise manner. If it

doesn’t exist, return -1 for this element.

Practice Here

Question 9.

Design your implementation of the circular queue.

The circular queue is a linear data structure in

which the operations are performed based on FIFO

(First In First Out) principle, and the last position is

connected back to the first position to make a circle.

It is also called "Ring Buffer".

Practice Here
Question 10.
You are given an array of integers nums, there is a
sliding window of size k which is moving from the
very left of the array to the very right. You can only
see the k numbers in the window. Each time the
sliding window moves right by one position.

Return the max sliding window.

Practice Here

Question 11.
Design a stack that supports push, pop, top, and
retrieving the minimum element in constant time.

Practice Here
Question 12.
You are given an m x n grid where each cell can
have one of three values:

0 representing an empty cell,

1 representing a fresh orange, or

2 representing a rotten orange.

Every minute, any fresh orange that is 4-


directionally adjacent to a rotten orange becomes
rotten.

Return the minimum number of minutes that must


elapse until no cell has a fresh orange. If this is
impossible, return -1.

Practice Here
TREES & BINARY SEARCH
TREES
Question 1.
Given the root of a binary tree, return its maximum
depth.

A binary tree's maximum depth is the number of


nodes along the longest path from the root node
down to the farthest leaf node.
Practice Here

Question 2.
Given the root of a binary tree, invert the tree, and
return its root.
Practice Here
Question 3.

A path in a binary tree is a sequence of nodes where

each pair of adjacent nodes in the sequence has an

edge connecting them. A node can only appear in

the sequence at most once. Note that the path does

not need to pass through the root.

The path sum of a path is the sum of the node's

values in the path.

Given the root of a binary tree, return the maximum

path sum of any non-empty path.

Practice Here

Question 4.

Given the roots of two binary trees p and q, write a

function to check if they are the same or not.

Two binary trees are considered the same if they are

structurally identical, and the nodes have the same

value.

Practice Here
Question 5.
Given the root of a binary tree, return the level order
traversal of its nodes' values. (i.e., from left to right,
level by level).

Practice Here

Question 6.
Given a binary tree, find the lowest common
ancestor (LCA) of two given nodes in the tree.

Practice Here

Question 7.
Given two integer arrays preorder and inorder
where preorder is the preorder traversal of a binary
tree and inorder is the inorder traversal of the same
tree, construct and return the binary tree.

Practice Here
Question 8.

Given the root of a binary tree, imagine yourself

standing on the right side of it, return the values of

the nodes you can see ordered from top to bottom.

Practice Here

Question 9.

Given the root of a binary tree, determine if it is a

valid binary search tree (BST).

Practice Here

Question 10.

Given the root of a binary search tree, and an

integer k, return the kth smallest value (1-indexed)

of all the values of the nodes in the tree.

Practice Here
Question 11.

Given the root of a binary tree, return the zigzag

level order traversal of its nodes' values. (i.e., from

left to right, then right to left for the next level and

alternate between).

Practice Here

Question 12.

Given a binary tree, determine if it is height-

balanced

Practice Here
TRIES
Question 1.
A trie (pronounced as "try") or prefix tree is a tree
data structure used to efficiently store and retrieve
keys in a dataset of strings. There are various
applications of this data structure, such as
autocomplete and spellchecker.

Implement the Trie.

Practice Here

Question 2.
Design a data structure that supports adding new
words and finding if a string matches any previously
added string.

Practice Here
Question 3.

Given an integer array nums, return the maximum

result of nums[i] XOR nums[ j], where 0 <= i <= j < n.

Practice Here

Question 4.

Given a string s and a dictionary of strings wordDict,

return true if s can be segmented into a space-

separated sequence of one or more dictionary

words.

Note that the same word in the dictionary may be

reused multiple times in the segmentation.

Practice Here
Question 5.
Given an m x n board of characters and a list of
strings words, return all words on the board.

Each word must be constructed from letters of


sequentially adjacent cells, where adjacent cells are
horizontally or vertically neighboring. The same
letter cell may not be used more than once in a
word.

Practice Here
HEAPSt
Question 1.
You are given an array of k linked-lists lists, each
linked-list is sorted in ascending order.

Merge all the linked-lists into one sorted linked-list


and return it.

Practice Here

Question 2.
Given an integer array nums and an integer k,
return the k most frequent elements. You may
return the answer in any order.

Practice Here
Question 3.

Given a string s, rearrange the characters of s so that

any two adjacent characters are not the same.

Return any possible rearrangement of s or return ""

if not possible.

Practice Here

Question 4.

The median is the middle value in an ordered

integer list. If the size of the list is even, there is no

middle value, and the median is the mean of the

two middle values.

Implement the MedianFinder class:

Practice Here
GRAPHS
Question 1.
Given an m x n 2D binary grid grid which represents
a map of '1's (land) and '0's (water), return the
number of islands.

An island is surrounded by water and is formed by


connecting adjacent lands horizontally or vertically.
You may assume all four edges of the grid are all
surrounded by water.

Practice Here

Question 2.
Given an m x n binary matrix mat, return the
distance of the nearest 0 for each cell.

The distance between two adjacent cells is 1.

Practice Here
Question 3.

Given a graph, traverse through all the nodes in the

graph using Depth First Search and Breadth First

Search

Practice Here

Question 4.

Given a weighted graph, implement Dijkstra's

shortest path.

Practice Here

Question 5.

Given a weighted, undirected, and connected graph

of V vertices and E edges. The task is to find the

sum of weights of the edges of the Minimum

Spanning Tree

Practice Here
Question 6.

Given is a 2D adjacency list representation of a

graph. Check whether the graph is a Bipartite

graph.

Practice Here

Question 7.

Given the root of a Directed graph, check whether

the graph contains a cycle if yes then return true,

return false otherwise.

Practice Here

Question 8.

Given a Directed Graph with V vertices (Numbered

from 0 to V-1) and E edges, Find the number of

strongly connected components in the graph.

Practice Here
Question 9.
An image is represented by an m x n integer grid
image where image[i][j] represents the pixel value
of the image.

You are also given three integers sr, sc, and color.
You should perform a flood fill on the image starting
from the pixel image[sr][sc].

Practice Here

Question 10.
Given a graph, find the topological order for the
given graph.t

Practice Here
DYNAMIC PROGRAMMING
AND GREEDY
Question 1.
Given an integer array nums, find a subarray that
has the largest product, and return the product.
Practice Here

Question 2.
Given an integer array nums, return the length of
the longest strictly increasing subsequence
Practice Here
Question 3.

Given two strings word1 and word2, return the

minimum number of operations required to convert

word1 to word2.

You have the following three operations permitted

on a word:

Insert a character

Delete a character

Replace a character

Practice Here

Question 4.

Given a non-empty array nums containing only

positive integers, find if the array can be partitioned

into two subsets such that the sum of elements in

both subsets is equal.

Practice Here
Question 5.
You are given an integer array coins representing
coins of different denominations and an integer
amount representing a total amount of money.

Return the fewest number of coins that you need to


make up that amount. If that amount of money
cannot be made up by any combination of the
coins, return -1.

You may assume that you have an infinite number


of each kind of coin.

Practice Here

Question 6.
Given two strings text1 and text2, return the length
of their longest common subsequence. If there is no
common subsequence, return 0.

Practice Here
Question 7.

There is a robot on an m x n grid. The robot is

initially located at the top-left corner (i.e., grid[0][0]).

The robot tries to move to the bottom-right corner

(i.e., grid[m - 1][n - 1]). The robot can only move either

down or right at any point in time.

Given the two integers m and n, return the number

of possible unique paths that the robot can take to

reach the bottom-right corner.

Practice Here

Question 8.

Given a string s and a dictionary of strings wordDict,

return true if s can be segmented into a space-

separated sequence of one or more dictionary

words.

Practice Here
Question 9.
Given a set of N jobs where each job has a deadline
and profit associated with it.

Each job takes 1 unit of time to complete and only


one job can be scheduled at a time. We earn the
profit associated with job if and only if the job is
completed by its deadline.

Find the number of jobs done and the maximum


profit.

Practice Here

Question 10.
Given weights and values of N items, we need to
put these items in a knapsack of capacity W to get
the maximum total value in the knapsack.

Practice Here
Why

Bosscoder?
1000+ Alumni placed at Top
Product-based companies.

More than 136% hike for every 



2 out of 3 working professional.

Average package of 24LPA.

Explore More

You might also like