All Questions
All Questions
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.
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.
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
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
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.