DSA
DSA
Interview Questions
Most asked in
Example:
Input: s = "abcabcbb"
Output: 3
Problem link
created by Heycoach 1
Question 2
Zigzag Conversion
Example:
Output: "PAHNAPLSIIGYIR"
P A H N
A P L S I I G
Y I R
Write the code that will take a string and make this
conversion given a number of rows.
Problem link
String to Integer
Example 1:
Input: s = "42"
Output: 42
Example 2:
Output: -42
Problem link
Example:
Output: 3
Problem link
5) Next Permutation
Example 1:
Output: [1,3,2]
Example 2:
Output: [1,2,3]
Problem link
created by Heycoach 5
Question 6
Example 1:
Output: 4
Example 2:
Output: -1
Problem link
Group Anagrams
Example:
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Problem link
Pow(x,n)
Example 2:
Input: x = 2.00000, n = -2
Output: 0.25000
Problem link
Spiral Matrix
Example 1:
Output: [1,2,3,6,9,8,7,4,5]
Problem link
created by Heycoach 9
Question 10
Jump Game
Example 1:
Output: true
Problem link
Merge Intervals
Example:
Output: [[1,6],[8,10],[15,18]]
Problem link
Edit Distance
Example:
Output: 3
Explanation:
Problem link
created by Heycoach 12
Question 13
Search a 2D Matrix
Example:
Problem link
Word Search
Example:
Output: true
Problem link
Decode Ways
'A' -> "1", 'B' -> "2" , ..., 'Z' -> "26"
Example:
Input: s = "226"
Output: 3
Problem link
created by Heycoach 15
Question 16
Example:
Input: n = 3
Output: 5
Problem link
Interleaving String
s = s1 + s2 + ... + sn
t = t1 + t2 + ... + tm
|n - m| <= 1
Example:
Output: true
Problem link
You are given the root of a binary search tree (BST), where
the values of exactly two nodes of the tree were swapped
by mistake. Recover the tree without changing its structure.
Example:
Output: [2,1,4,null,null,3]
created by Heycoach 18
Question 19
Example:
Output: [3,9,20,null,null,15,7]
Problem link
Given the root of a binary tree, flatten the tree into a "linked
list":
The "linked list" should use the same TreeNode class where
the right child pointer points to the next node in the list and
the left child pointer is always null.
Example:
Output: [1,null,2,null,3,null,4,null,5,null,6]
Problem link
Example:
Output: [1,#,2,3,#,4,5,7,#]
Problem link
created by Heycoach 21
Question 22
Surrounded Regions
Example:
Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],
["X","O","X","X"]]
- It is on the border, or
Problem link
Gas Station
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from the ith station to its next (i + 1)th
station. You begin the journey with an empty tank at one of
the gas stations.
Given two integer arrays gas and cost, return the starting
gas station's index if you can travel around the circuit once
in the clockwise direction, otherwise return -1. If there exists
a solution, it is guaranteed to be unique.
Example 1:
Output: 3
Problem link
Example:
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Problem link
Word Break
Example:
Output: true
Problem link
created by Heycoach 25
Question 26
LRU Cache
b) int get(int key) Return the value of the key if the key
exists, otherwise return -1.
The functions get and put must each run in O(1) average
time complexity.
Example:
Input:
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output:
Problem link
Sort List
Given the head of a linked list, return the list after sorting it
in ascending order.
Example:
Output: [1,2,3,4]
Problem link
Min Stack
b) void push(int val) pushes the element val onto the stack.
Example:
Input:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output:
[null,null,null,null,-3,null,0,-2]
Problem link
created by Heycoach 28
Question 29
Example:
Input:
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
Output:
Problem link
Course Schedule
For example, the pair [0, 1], indicates that to take course 0
you have to first take course 1. Return true if you can finish
all courses. Otherwise, return false.
Example 1:
Output: true
Problem link
Example:
Output: 4
Problem link
created by Heycoach 31
Question 32
b) bool insert(int val) Inserts an item val into the set if not
present. Returns true if the item was not present, false
otherwise.
Example:
Input:
Output:
Problem link
Given the head of a linked list, return the node where the
cycle begins. If there is no cycle, return null. Do not modify
the linked list.
Example:
Problem link
created by Heycoach 33
Question 34
Example 1:
Output: 6
Example 2:
Output: 0
Problem link
Example 1:
Output: 2.00000
Example 2:
Output: 2.50000
Problem link
Given the head of a linked list, reverse the nodes of the list k
at a time, and return the modified list.
You may not alter the values in the list's nodes, only nodes
themselves may be changed.
Example:
Output: [2,1,4,3,5]
Problem link
created by Heycoach 36
Question 37
Example 1:
Output: 3
Problem link
Example:
Output: 6
Problem link
Wildcard Matching
Example 2:
Output: false
Problem link
N-Queens
Example:
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Problem link
created by Heycoach 40
Question 41
Example:
Output: "BANC"
Problem link
Example:
Output: 10
Problem link
Distinct Subsequences
Example:
Output: 3
Explanation:
"rabbit" from s.
Problem link
created by Heycoach 43
Question 44
Example:
Output: 6
Problem link
Example:
Output: 42
Problem link
Word Ladder
Example:
Output: 5
Problem link
created by Heycoach 46
Question 47
Palindrome Partitioning II
Example:
Input: s = "aab"
Output: 1
Problem link
Candy
Example:
Output: 5
Problem link
Example:
Output: [3,3,5,5,6,7]
Explanation:
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Problem link
created by Heycoach 49
Question 50
Example:
Input:
Output:
Problem link