0% found this document useful (0 votes)
41 views15 pages

All Questions

Leetcode questions with their answers

Uploaded by

Diego Andres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views15 pages

All Questions

Leetcode questions with their answers

Uploaded by

Diego Andres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 15

DAY TOPIC Question - Short

DAY1 ARRAY Sorted Squared Array


DAY1 ARRAY Monotonic Array
DAY2 ARRAY Rotate Array

DAY2 ARRAY Container with most


DAY3 HASH TABLE Two Sum

DAY3 HASH TABLE Isomorphic Strings


DAY4 RECURSION Fibonacci

DAY4 RECURSION Power Sum


DAY5 RECURSION Permutations

DAy5 RECURSION Power Set


DAY6 STRING Non repeating charac
DAY6 STRING Palindrome
DAY7 STRING Longest Unique char S
DAY7 STRING Group Anagrams
DAY8 SEARCHING Binary Search

DAY8 SEARCHING Search in Rotated Sor


DAY9 SEARCHING Find First and Last Po

DAY9 SEARCHING Search in 2D Array


DAY10 SORTING Bubble Sort
DAY10 SORTING Insertion Sort
DAY11 SORTING Selection Sort
DAY11 SORTING Merge Sort
DAY12 SORTING Quick Sort
DAY12 SORTING Radix Sort
DAY13 SINGLY LINKED LIST Construct SLL
DAY13 SINGLY LINKED LIST delete duplicates
DAY14 SINGLY LINKED LIST Reverse SLL
DAY14 SINGLY LINKED LIST Cycle Detection
DAY15 SINGLY LINKED LIST find duplicate numbe

DAY15 SINGLY LINKED LIST Add 2 numbers

DAY16 DOUBLY LINKED LIST DLL remove insert

DAY16 DOUBLY LINKED LIST DLL remove all, insert

DAY17 STACKS Construct Stack

DAY17 STACKS Reverse Polish Notati

DAY18 QUEUES Construct Queue


DAY18 QUEUES Implement Queue wit

DAY19 BINARY TREE AND BS Construct BST

DAY19 BINARY TREE AND BS Traverse BST

DAY20 BINARY TREE AND BS Level Order traversal

DAY20 BINARY TREE AND BS Left/Right View of bin

DAY21 BINARY TREE AND BS Invert Binary Tree


DAY21 BINARY TREE AND BS Diameter of binary tr
DAY22 BINARY TREE AND BS Convert Sorted Array

DAY22 BINARY TREE AND BS Validate BST

DAY23 HEAPS AND PRIORITYMax Heap Constructi

DAY23 HEAPS AND PRIORITYmin Priority Queue Co


DAy24 GRAPHS BFS - Adj List and Adj

DAY24 GRAPHS DFS - Iterative and Re

DAY25 GRAPHS Number of connected

DAY25 GRAPHS Course Scheduler / To


Question - Long
You are given an array of Integers in which each subsequent value is not less than the previous value. Write a function that tak
array with the squares of each number sorted in ascending order.
An array is monotonic if it is either monotone increasing or monotone decreasing. An array is monotone increasing if all its ele
Given an array, rotate the array to the right by k steps, where k is non-negative.
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 a
Find two lines that together with the x-axis form a container, such that the container contains the most water(depth is consta
the 2 lines and the X axis make) of container which can store the max amount of water. Notice that you may not slant the con
You are given an array of Integers and another integer targetValue. Write a function that will take these inputs and return the
Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be rep
All occurrences of a character must be replaced with another character while preserving the order of characters. No two chara
character may map to itself. s and t consist of any valid ascii character.
In the Fibonacci sequence, each subsequent term is obtained by adding the preceding 2 terms. This is true for all the numbers

Let’s define a peculiar type of array in which each element is either an integer or another peculiar array. Assume that a peculia
will take a peculiar array as its input and find the sum of its elements. If an array is an element in the peculiar array you have t
you can sum it with the other elements. Equivalent value of an array is the sum of its elements raised to the number which rep
[2,3[4,1,2]] = 2+3+ (4+1+2)^2
[1,2,[7,[3,4],2]] = 1 + 2 +( 7+(3+4)^3+2)^2
Given an array of distinct integers, return all the possible permutations. You can return the answer in any order.
Given an integer array of unique elements, return all possible subsets (the power set). The solution set must not contain dupli
order.
You are given a string consisting of only lower case and upper-case English Alphabets and integers 0 to 9. Write a function tha
You are given a string. Write a function to check whether the string is a palindrome or not.
Given a string s, find the length of the longest substring without repeating characters.
Given an array of strings consisting of lower case English letters, group the anagrams together. You can return the answer in a
Given an array of integers which is sorted in ascending order, and a target integer, write a function to search for whether the

You are given an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nu
index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[
For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Given an integer target, return the in
1. You must write an algorithm with O(log n) runtime complexity.
You are given an array of integers sorted in non-decreasing order, find the starting and ending position of a given target value

Write an efficient algorithm that searches for a value target in an m x n integer matrix. This matrix has the following propertie
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.
If the value is there in the matrix return true, else false.
You are given an array of integers. Write a function that will take this array as input and return the sorted array using Bubble s
You are given an array of integers. Write a function that will take this array as input and return the sorted array using Insertion
You are given an array of integers. Write a function that will take this array as input and return the sorted array using Selection
You are given an array of integers. Write a function that will take this array as input and return the sorted array using Merge s
You are given an array of integers. Write a function that will take this array as input and return the sorted array using Quick so
You are given an array of non-negative integers. Write a function that will take this array as input and return the sorted array u
Design a Singly linked list class that has a head and tail. Every node is to have two attributes: value: the value of the current n
The linked list is to be 0-indexed. The class should support the following:
SinglyLinkedList() Initializes the SinglyLinkedList object.
get(index) Get the value of the indexth node. If the index is invalid, return -1.
addAtHead(value)- Add a node of given value before the first element of the linked list.
addAtTail(value) - Add a node of given value at the last element of the linked list.
addAtIndex(index, value) Add a node of given value before the indexth node in the linked list. If index equals the length of the
end of the linked list. If index is greater than the length, don’t insert the node.
deleteAtIndex(index) Delete the indexth node in the linked list, if the index is valid, else nothing happens.
You are given the head of a Sorted Singly Linked list. Write a function that will take the given head as input, delete all nodes th
You are given the head of a Singly Linked list. Write a function that will take the given head as input, reverse the Linked List an
You are given the head of a linked list. Check if there is a cycle and if yes, return the node where the cycle begins. If there is n
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and e
the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself. 0<=Node value<=9

Create a Doubly Linked List class. Write Instance Methods for this class to be able to
1.remove a node when the node to be removed is given as Input.
2. insert a node before a particular node(both the node to be inserted and the node before which the insertion is to happen w
inserted is
-part of the linked list then shift its place to the desired location
-a new node, then insert the new node at the place desired.

Create a Doubly Linked List class. Write Instance Methods for this class to be able to
1. remove all the nodes in the doubly linked list which have their value equal to a given value.
2.Insert a node at a desired position (node and position are given). The Linked List is 0 indexed. If given node is a node existing
position.

Implement a Stack:
Using an Array
with a Stack class using a Linked list
One should be able to add to the stack and remove from the stack following the LIFO property.
Evaluate the value of an arithmetic expression in Reverse Polish Notation(See example). Valid operators are +, -, *, and /. Note
truncate toward zero. It is guaranteed that the given RPN expression is always valid. That means the expression would always
any division by zero operation. The Input is an array of strings where each element is either a valid operator or an integer. E.g.

Implement a Queue:
Using an Array
with a Queue class using a Linked list
One should be able to add to the queue and remove from the queue following the FIFO property.
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a
Implement the MyQueue class:
push(val) Pushes element val to the back of the queue.
pop() Removes the element from the front of the queue and returns it.
peek() Returns the element at the front of the queue.
empty() Returns true if the queue is empty, false otherwise.

Notes:
You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty opera
Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-
stack's standard operations.

Follow-up: Implement the queue such that each operation is amortized O(1) time complexity. In other words, performing n op
one of those operations may take longer.

Design a Binary Search Tree class that supports the following:


1.Insert a value
2.Remove a value. This method should remove the first occurrence of a value.
3.Find a value. If the value is found it should return the node with the value else return false.

Write a 4 instance methods for a Binary Search Tree class to traverse the BST.
1. Method 1 : traverse the tree breadth first and return an array that contains all the values of the BST.
2. Method 2: traverse the tree depth first – In-order and return an array that contains all the values of the BST.
3. Method 3 : traverse the tree depth first – Pre-order and return an array that contains all the values of the BST.
4. Method 4 : traverse the tree depth first – Post-order and return an array that contains all the values of the BST.
Write a function that takes the root of a binary tree, and returns the level order traversal of its nodes' values. (i.e., from left to
instance method for the Binary Search tree class to insert the values given as an array into the Binary tree (from left to right, le
not null is to be made a node and added to the tree. (See examples below). Then write the function mentioned first.
1. 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
2. Given the root of a binary tree, imagine yourself standing on the left side of it, return the values of the nodes you can see o
Given the root of a binary tree, invert the tree, and return its root.
(Invert means swap every left node for its corresponding right node / get mirror image)
Write a function which takes in the root of a binary tree and returns the length of the diameter of the tree. The diameter of a
You are given an array where the elements are strictly in increasing (ascending) order, convert it to a height-balanced binary s

You are given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.

Write a max Heap Class that supports the following:


1.Building a Max heap from an input array
2.Inserting integers in the Heap
3.Removing the Heap’s maximum / root value
4.Peeking at the Heap’s maximum / root value
The Heap is to be represented in the form of an array.
Implement a Priority Queue as a min Binary Heap. The Priority Queue class should support the following functions
1.Enqueue to insert an element
2.Dequeue to extract the element with the highest priority ( lowest numerical priority is treated as highest priority)
You are given an undirected graph stored
as an adjacency list
as an adjacency Matrix.
Write functions to traverse this graph using the Breadth first Search approach. As you traverse the graph store the values of th

You are given a graph stored as an adjacency list. Write functions to traverse the graph using the Depth first Search approach
1) recursively and
2) iteratively.
As you traverse the graph store the values of the vertices in an array and return this array.
You are given a graph with n vertices. To indicate the connections in the graph you are given an array edges whose each elem
indicates that there is an edge between u and v where u and v denote two vertices or nodes. Write a function that takes in ‘n’
number of connected components in the graph.
You have to take a total of n courses leabled from 0 to n-1. Before you can take some courses you need to take it’s prerequisit
prerequisites where each element [x,y] indicates that to take course x you have to take y first. E.g. [2,3] indicates that to take
a function that takes in n and the prerequisite array and returns true if you can complete all courses, else return false.
ecreasing. An array is monotone decreasing if all its elements from left to right are non-increasing. Given an integer array return true if th

ay that add up targetValue.

Fibonacci series as they do not have 2 preceding numbers. The first 2 terms in the Fibonacci series is 0 and 1. F(n) = F(n-1)+F(n-2) for n>1. W

eturn the index of the first character that is non-repeating.

phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
array. If it is there then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.

return [-1, -1]. You must write an algorithm with O(log n) runtime complexity.
value of another node so that each value appears 1 time only and return the linked list, which is still to be a sorted linked list.
rsed Linked List.
e in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Do not modify the lin
this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space.
gest path between any two nodes in the tree. It is not necessary for this path to pass through the root of the tree. The length of a path be
ary tree is a binary tree in which the depth of the two subtrees of every node does not differ by more than 1.
n integer array return true if the given array is monotonic, or false otherwise.

. F(n) = F(n-1)+F(n-2) for n>1. Write a function that finds F(n) given n where n is an integer greater than equal to 0. For the first term n = 0.

time complexity.
sorted linked list.

t pointer. Do not modify the linked list.


nt extra space.
e tree. The length of a path between two nodes is the number of edges between them.
al to 0. For the first term n = 0.

You might also like