Most of These problem are found on LeetCode with minor changes in Output/Input.
Solutions might not be the optimised one , but they passed all leetcode testcase within given time limit.
Click here for solution.
Click here for solution.
For example, given the regular expression "ra." and the string "ray", your function should return true.
Click here for solution.
Click here for solution.
NOTE in the above question we have to find kth smallest number , so after a little bit of change we can also find k-th largest number and for this question k=2.
Using a function rand7() that returns an integer from 1 to 7 (inclusive) with uniform probability, implement a function rand5() that returns an integer from 1 to 5 (inclusive).
Click here for solution.
A knight's tour is a sequence of moves by a knight on a chessboard such that all squares are visited once.
input : 1 , Output : 1
input : 2 , Output : 0
input : 3 , Output : 0
input : 4 , Output : 0
input : 5 , Output : 1728 (it took 23 seconds)
Click here for solution.
[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]]
1
2
3
4
5
10
15
20
19
18
17
16
11
6
7
8
9
14
13
12
Click here for solution.
On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces.
You are given N bishops, represented as (row, column) tuples on a M by M chessboard. Write a function to count the number of pairs of bishops that attack each other. The ordering of the pair doesn't matter: (1, 2) is considered the same as (2, 1).
(0, 0)
(1, 2)
(2, 2)
(4, 0)
[b 0 0 0 0]
[0 0 b 0 0]
[0 0 b 0 0]
[0 0 0 0 0]
[b 0 0 0 0]
Click here for solution.
Given a list of integers, return the largest product that can be made by multiplying any three integers.
Click here for solution.
Click here for solution.
Using a function rand7() that returns an integer from 1 to 7 (inclusive) with uniform probability, implement a function rand5() that returns an integer from 1 to 5 (inclusive).
Click here for solution.
In a directed graph, each node is assigned an uppercase letter. We define a path's value as the number of most frequently-occurring letter along that path. For example, if a path in the graph goes through "ABACA", the value of the path is 3, since there are 3 occurrences of 'A' on the path.
Given a graph with n nodes and m directed edges, return the largest value path of the graph. If the largest value is infinite, then return null.
The graph is represented with a string and an edge list. The i-th character represents the uppercase letter of the i-th node. Each tuple in the edge list (i, j) means there is a directed edge from the i-th node to the j-th node. Self-edges are possible, as well as multi-edges.
ABACA
[ (0, 1),
(0, 2),
(2, 3),
(3, 4) ]
A
[ (0, 0) ]
Click here for solution.
Click here for solution.
Suppose you have a multiplication table that is N by N. That is, a 2D array where the value at the i-th row and j-th column is (i + 1) * (j + 1) (if 0-indexed) or i * j (if 1-indexed).
Given integers N and X, write a function that returns the number of times X appears as a value in an N by N multiplication table.
For example, given N = 6 and X = 12, you should return 4, since the multiplication table looks like this:
| 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | 4 | 6 | 8 | 10 | 12 |
| 3 | 6 | 9 | 12 | 15 | 18 |
| 4 | 8 | 12 | 16 | 20 | 24 |
| 5 | 10 | 15 | 20 | 25 | 30 |
| 6 | 12 | 18 | 24 | 30 | 36 |
Click here for solution.
Given an array of numbers, find the length of the longest increasing subsequence in the array. The subsequence does not necessarily have to be contiguous.
For example, given the array [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], the longest increasing subsequence has length 6: it is 0, 2, 6, 9, 11, 15.
Click here for solution.
You are given an N by M 2D matrix of lowercase letters. Determine the minimum number of columns that can be removed to ensure that each row is ordered from top to bottom lexicographically. That is, the letter at each column is lexicographically later as you go down each row. It does not matter whether each row itself is ordered lexicographically.
cba daf ghi
This is not ordered because of the a in the center. We can remove the second column to make it ordered:
ca df gi
abcdef
zyx wvu tsr