#Daily Coding Problem
This are solution to Daily Coding Problem.
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.
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
Click here for solution.
cons(a, b)
constructs a pair, and car(pair)
and cdr(pair)
returns the first and last element of that pair. For example, car(cons(3, 4))
returns 3
, and cdr(cons(3, 4))
returns 4
.
Click here for solution.
An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding next
and prev
fields, it holds a field named both
, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an add(element)
which adds the element to the end, and a get(index)
which returns the node at index.
If using a language that has no pointers (such as Python), you can assume you have access to get_pointer
and dereference_pointer
functions that converts between nodes and memory addresses.
Click here for solution.
For example, given the regular expression "ra."
and the string "ray"
, your function should return true
.
Click here for solution.
2
1.5
2
3.5
2
2
2
Click here for solution.
segregate the values of the array so that all the Rs
come first, the Gs
come second, and the Bs
come last.
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
Click here for solution.
Given a list of possibly overlapping intervals, return a new list of intervals where all overlapping intervals have been merged.
For example, given [(1, 3), (5, 8), (4, 10), (20, 25)]
, you should return [(1, 3), (4, 10), (20, 25)]
.
Click here for solution.
Given k
sorted singly linked lists, write a function to merge all the lists into one
sorted singly linked list.
Click here for solution.
Given an array of integers, write a function to determine whether the array could become non-decreasing by modifying at most 1 element.
For example, given the array [10, 5, 7]
, you should return true, since we can modify the 10
into a 1
to make the array non-decreasing.
Given the array [10, 5, 1]
, you should return false, since we can't modify any one element to get a non-decreasing array.
Click here for solution.
Given the root of a binary tree, return a deepest node. For example, in the following tree, return d
.
a
/ \
b c
/
d
Click here for solution.
Given a mapping of digits to letters (as in a phone number)
, and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.
For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …}
then “23”
should return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"]
.
Click here for solution.
Using a read7()
method that returns 7
characters from a file, implement readN(n)
which reads n
characters.
For example, given a file with the content “Hello world
”, three read7()
returns “Hello w
”, “orld
” and then “”.
Click here for solution.
a
/ \
b c
/ \ /
d e f
a
/ \
c b
\ / \
f e d
Click here for solution.
Given a matrix of 1s and 0s, return the number of "islands" in the matrix. A 1
represents land and 0
represents water, so an island is a group of 1s that are neighboring whose perimeter is surrounded by water.
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
1 1 0 0 1
1 1 0 0 1
Click here for solution.
Given three 32
-bit integers x
, y
, and b
, return x
if b
is 1
and y
if b
is 0
, using only mathematical or bit operations. You can assume b
can only be 1
or 0
.
Click here for solution.
Given a string of parentheses, write a function to compute the minimum number of parentheses to be removed to make the string valid (i.e. each open parenthesis is eventually closed).
For example, given the string "()())()"
, you should return 1
. Given the string ")("
, you should return 2
, since we must remove all of them.
Click here for solution.
A N B
B NE C
C N A
A NW B
A N B
Click here for solution.
Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder.
Click here for solution.
A binary search tree is a tree with two children, left
and right
, and satisfies the constraint that the key in the left child must be less than or equal to
the root and the key in the right child must be greater than or equal to
the root.
Click here for solution.
Given an integer n
and a list of integers l
, write a function that randomly generates a number from 0
to n-1
that isn't in l (uniform)
.
Click here for solution.
What does the below code snippet print out? How can we fix the anonymous functions to behave as we'd expect?
functions = []
for i in range(10):
functions.append(lambda : i)
for f in functions:
print(f())
Click here for solution.
We're given a hashmap
associating each courseId key
with a list of courseIds values
, which represents that the prerequisites
of courseId are courseIds. Return a sorted ordering of courses
such that we can finish all courses.
{
'CSC300': ['CSC100', 'CSC200'],
'CSC200': ['CSC100'],
'CSC100': []
}
Click here for solution.
I didn't find any matching problem on LeetCode but you can try course schedule,course schedule II.
Click here for solution.
Click here for solution.
Given a number represented by a list of digits, find the next greater permutation of a number, in terms of lexicographic ordering. If there is not greater permutation possible, return the permutation with the lowest value/ordering
.
For example, the list [1,2,3]
should return [1,3,2]
. The list [1,3,2]
should return [2,1,3]
. The list [3,2,1]
should return [1,2,3]
.
Click here for solution.
Click here for solution.
Write a map implementation with a get function that lets you retrieve the value of a key at a particular time.
The map should work like this. If we set a key at a particular time, it will maintain that value forever or until it gets set at a later time. In other words, when we get a key at a time, it should return the value that was set for that key set at the most recent time
.
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 2) # set key 1 to value 2 at time 2
d.get(1, 1) # get key 1 at time 1 should be 1
d.get(1, 3) # get key 1 at time 3 should be 2
d.set(1, 1, 5) # set key 1 to value 1 at time 5
d.get(1, 0) # get key 1 at time 0 should be null
d.get(1, 10) # get key 1 at time 10 should be 1
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 0) # set key 1 to value 2 at time 0
d.get(1, 0) # get key 1 at time 0 should be 2
Click here for solution.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
Click here for solution.
For example, given [100, 4, 200, 1, 3, 2]
, the longest consecutive element sequence is [1, 2, 3, 4]
. Return its length: 4
.
Click here for solution.
(x,y)
to
(x+1, y),
(x - 1, y),
(x, y+1),
(x, y-1),
(x-1, y-1),
(x+1,y+1),
(x-1,y+1),
(x+1,y-1)
You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first point.
It takes 1
step to move from (0, 0)
to (1, 1)
. It takes one more step to move from (1, 1)
to (1, 2)
.
Click here for solution.
Given an even number (greater than 2
), return two prime numbers whose sum will be equal to the given number.
A solution will always exist. See Goldbach’s conjecture.
Click here for solution.
For example, if the list is [1, 2, 3, 4, 5]
and K
is 9
, then it should return [2, 3, 4]
, since 2 + 3 + 4 = 9
.
Click here for solution.
Given a string and a set of characters, return the shortest substring containing all the characters in the set.
For example, given the string "figehaeci"
and the set of characters {a, e, i}
, you should return "aeci"
.
Click here for solution.
Click here for solution.
That is, as long as the debounced f
continues to be invoked, f
itself will not be called for N
milliseconds.
Click here for solution.
Given an integer list where each number represents the number of hops you can make, determine whether you can reach to the last index starting at index 0
.
Click here for solution.
1
/ \
2 3
/ \
4 5
Click here for solution.
Click here for solution.
Given an unsigned 8-bit integer
, swap its even and odd bits
. The 1st and 2nd bit should be swapped, the 3rd and 4th bit should be swapped, and so on.
Click here for solution.
1
/ \
2 3
/ \
4 5
Click here for solution.
Click here for solution.
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Assume that each node in the tree also has a pointer to its parent.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
Click here for solution.
Given a string of words delimited by spaces, reverse the words in string. For example, given "hello world here"
, return "here world hello"
Click here for solution.
Given a string and a set of delimiters, reverse the words in the string while maintaining the relative order of the delimiters
. For example, given "hello/world:here"
, return "here/world:hello"
Follow-up: Does your solution work for the following cases: "hello/world:here/"
, "hello//world:here"
Click here for solution.
Given two non-empty binary trees s
and t
, check whether tree t
has exactly the same structure and node values with a subtree of s
. A subtree of s
is a tree consists of a node in s
and all of this node’s descendants. The tree s
could also be considered as a subtree of itself.
Click here for solution.
Click here for solution.
Click here for solution.
Click here for solution.
Given a set of closed intervals, find the smallest set of numbers that covers all the intervals. If there are multiple smallest sets, return any of them.
For example, given the intervals [0, 3], [2, 6], [3, 4], [6, 9]
one set of numbers that covers all these intervals is {3, 6}
.
Click here for solution.
Implement the singleton pattern with a twist. First, instead of storing one instance, store two instances
. And in every even
call of getInstance()
, return the first instance
and in every odd call
of getInstance(), return the second instance
.
Click here for solution.
You are given a 2-d matrix where each cell represents number of coins in that cell. Assuming we start at matrix[0][0], and can only move right or down, find the maximum number of coins you can collect by the bottom right corner.
0 3 1 1
2 0 0 4
1 5 3 1
Click here for solution.
Click here to visit for this question.
You have n
fair coins and you flip them all at the same time. Any that come up tails you set aside. The ones that come up heads you flip again. How many rounds do you expect to play before only one coin remains?
Write a function that, given n
, returns the number of rounds you'd expect to play until one coin remains.
Click here for solution.
All the disks start off on the first rod in a stack. They are ordered by size, with the largest disk on the bottom and the smallest one at the top.
The goal of this puzzle is to move all the disks from the first rod to the last rod while following these rules:
A move consists of taking the uppermost disk from one of the stacks and placing it on top of another stack.
Write a function that prints out all the steps necessary to complete the Tower of Hanoi. You should assume that the rods are numbered, with the first rod being 1
, the second (auxiliary) rod being 2
, and the last (goal) rod being 3
.
Move 1 to 3
Move 1 to 2
Move 3 to 2
Move 1 to 3
Move 2 to 1
Move 2 to 3
Move 1 to 3
Click here for solution.
Given a node in a binary search tree, return the next bigger element, also known as the inorder successor.
10
/ \
5 30
/ \
22 35
Click here for solution.
Click here to visit for this question.
Click here for solution.
Given an N by M matrix consisting only of 1's and 0's, find the largest rectangle containing only 1's and return its area.
[[1, 0, 0, 0],
[1, 0, 1, 1],
[1, 0, 1, 1],
[0, 1, 0, 0]]
Click here for solution.
Click here to visit for this question.
Implement a 2D iterator class. It will be initialized with an array of arrays, and should implement the following methods:
* next()
: returns the next element in the array of arrays. If there are no more elements, raise an exception.
For example, given the input [[1, 2], [3], [], [4, 5, 6]]
, calling next()
repeatedly should output 1, 2, 3, 4, 5, 6
.
Click here for solution.
Click here to visit for this question.
Given a start word, an end word, and a dictionary of valid words, find the shortest transformation sequence from start to end such that only one letter is changed at each step of the sequence, and each transformed word exists in the dictionary. If there is no possible transformation, return null. Each word in the dictionary have the same length as start and end and is lowercase.
For example, given start = "dog"
, end = "cat"
, and dictionary = {"dot", "dop", "dat", "cat"}, return ["dog", "dot", "dat", "cat"]
.
Given start = "dog"
, end = "cat"
, and dictionary = {"dot", "tod", "dat", "dar"}
, return null
as there is no possible transformation from dog
to cat
.
Click here for solution.
Click here to visit for this question.
Alice wants to join her school's Probability Student Club. Membership dues are computed via one of two simple probabilistic games.
The first game: roll a die repeatedly. Stop rolling once you get a five followed by a six. Your number of rolls is the amount you pay, in dollars.
Which of the two games should Alice elect to play? Does it even matter? Write a program to simulate the two games and calculate their expected value.
Click here for solution.
Given a stack of N
elements, interleave the first half of the stack with the second half reversed using only one other queue. This should be done in-place.
For example, if the stack is [1, 2, 3, 4, 5]
, it should become [1, 5, 2, 4, 3]
. If the stack is [1, 2, 3, 4]
, it should become [1, 4, 2, 3]
.
Click here for solution.
Given two rectangles on a 2D graph, return the area of their intersection. If the rectangles don't intersect, return 0.
{
"top_left": (1, 4),
"dimensions": (3, 3) # width, height
}
and
{
"top_left": (0, 5),
"dimensions": (4, 3) # width, height
}
Click here for solution.
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
For example, given the intervals (7, 9), (2, 4), (5, 8)
, return 1
as the last interval can be removed and the first two won't overlap.
Click here for solution.
Let X be a set of n intervals on the real line. We say that a set of points P "stabs" X if every interval in X contains at least one point in P. Compute the smallest set of points that stabs X.
Click here for solution.
Given an integer n
, return the length of the longest consecutive run of 1s
in its binary representation.
Click here for solution.
Given a string with repeated characters, rearrange the string so that no two adjacent characters are the same. If this is not possible, return None.
Click here for solution.
insert(key: str, value: int)
: Set a given key's value in the map. If the key already exists, overwrite the value.
sum(prefix: str)
: Return the sum of all values of keys that begin with a given prefix.
mapsum.insert("columnar", 3)
assert mapsum.sum("col") == 3
mapsum.insert("column", 2)
assert mapsum.sum("col") == 5
Click here for solution.
In academia, the h-index is a metric used to calculate the impact of a researcher's papers. It is calculated as follows:
A researcher has index h
if at least h
of her N
papers have h
citations each. If there are multiple h
satisfying this formula, the maximum is chosen.
For example, suppose N = 5
, and the respective citations of each paper are [4, 3, 0, 1, 5]
. Then the h-index
would be 3
, since the researcher has 3
papers with at least 3
citations.
Click here for solution.
You are given an array of length 24, where each element represents the number of new subscribers during the corresponding hour. Implement a data structure that efficiently supports the following:
query(start: int, end: int)
: Retrieve the number of subscribers that have signed up between start
and end
(inclusive).
You can assume that all values get cleared at the end of the day, and that you will not be asked for start and end values that wrap around midnight.
Click here for solution.
The Sieve of Eratosthenes is an algorithm used to generate all prime numbers smaller than N. The method is to take increasingly larger prime numbers, and mark their multiples as composite.
For example, to find all primes less than 100, we would first mark [4, 6, 8, ...] (multiples of two), then [6, 9, 12, ...] (multiples of three), and so on. Once we have done this for all primes less than N, the unmarked numbers that remain will be prime.
Bonus: Create a generator that produces primes indefinitely (that is, without taking N
as an input).
Click here for solution.
The transitive closure of a graph is a measure of which vertices are reachable from other vertices. It can be represented as a matrix M, where M[i][j] == 1
if there is a path between vertices i
and j
, and otherwise 0
.
graph = [
[0, 1, 3],
[1, 2],
[2],
[3]
]
[1, 1, 1, 1]
[0, 1, 1, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
Click here for solution.
Given a linked list, rearrange the node values such that they appear in alternating low -> high -> low -> high ...
form. For example, given 1 -> 2 -> 3 -> 4 -> 5
, you should return 1 -> 3 -> 2 -> 5 -> 4
.
Click here for solution.
Given an array of integers out of order, determine the bounds of the smallest window that must be sorted in order for the entire array to be sorted. For example, given [3, 7, 5, 6, 9]
, you should return (1, 3)
.
Click here for solution.
In Ancient Greece, it was common to write text with the first line going left to right, the second line going right to left, and continuing to go back and forth. This style was called "boustrophedon".
1
/ \
2 3
/ \ / \
4 5 6 7
Click here for solution.
Ghost is a two-person word game where players alternate appending letters to a word. The first person who spells out a word, or creates a prefix for which there is no possible continuation, loses. Here is a sample game:
Given a dictionary of words, determine the letters the first player should start with, such that with optimal play they cannot lose.
For example, if the dictionary is ["cat", "calf", "dog", "bear"]
, the only winning start letter would be b
.
Click here for solution.
The sequence [0, 1, ..., N]
has been jumbled, and the only clue you have for its order is an array representing whether each number is larger or smaller than the last. Given this information, reconstruct an array that is consistent with it. For example, given [None, +, +, -, +]
, you could return [1, 2, 3, 0, 4]
.
Click here for solution.
Huffman coding is a method of encoding characters based on their frequency. Each letter is assigned a variable-length binary string, such as 0101
or 111110
, where shorter lengths correspond to more common letters. To accomplish this, a binary tree is built such that the path from the root to any leaf uniquely maps to a character. When traversing the path, descending to a left child corresponds to a 0
in the prefix, while descending right corresponds to 1
.
*
/ \
* *
/ \ / \
* a t *
/ \
c s
Given a dictionary of character frequencies, build a Huffman tree, and use it to determine a mapping between characters and their encoded binary strings.
Click here for solution.
A bridge in a connected (undirected) graph is an edge that, if removed, causes the graph to become disconnected. Find all the bridges in a graph.
Click here for solution.
Create a basic sentence checker that takes in a stream of characters and determines whether they form valid sentences. If a sentence is valid, the program should print it out.
2. All other characters must be lowercase letters, separators (,
,;
,:
) or terminal marks (.
,?
,!
,‽
).
Click here for solution.
MegaCorp wants to give bonuses to its employees based on how many lines of codes they have written. They would like to give the smallest positive amount to each worker consistent with the constraint that if a developer has written more lines of code than their neighbor, they should receive more money.
Given an array representing a line of seats of employees at MegaCorp, determine how much each one should get paid.
Click here for solution.
You are given an string representing the initial conditions of some dominoes. Each element can take one of three values:
Determine the orientation of each tile when the dominoes stop falling. Note that if a domino receives a force from the left and right side simultaneously, it will remain upright.
Click here for solution.
Write a function, throw_dice(N, faces, total)
, that determines how many ways it is possible to throw N
dice with some number of faces each to get a specific total.
Click here for solution.
A fixed point in an array is an element whose value is equal to its index. Given a sorted array of distinct elements, return a fixed point
, if one exists. Otherwise, return False
.
For example, given [-6, 0, 2, 40]
, you should return 2
. Given [1, 5, 7, 8]
, you should return False
.
Click here for solution.
You are given an array representing the heights of neighboring buildings on a city street, from east to west. The city assessor would like you to write an algorithm that returns how many of these buildings have a view of the setting sun, in order to properly value the street.
For example, given the array [3, 7, 8, 3, 6, 1]
, you should return 3
, since the top floors of the buildings with heights 8, 6, and 1
all have an unobstructed view to the west.
Click here for solution.
Click here for this question.
Given an array of integers, determine whether it contains a Pythagorean triplet. Recall that a Pythogorean triplet (a, b, c)
is defined by the equation a^2
+ b^2
= c^2
.
Click here for solution.
Click here for this question.
You are given a list of (website, user)
pairs that represent users visiting websites. Come up with a program that identifies the top k pairs of websites with the greatest similarity.
[('a', 1), ('a', 3), ('a', 5),
('b', 2), ('b', 6),
('c', 1), ('c', 2), ('c', 3), ('c', 4), ('c', 5)
('d', 4), ('d', 5), ('d', 6), ('d', 7),
('e', 1), ('e', 3), ('e': 5), ('e', 6)]
Then a reasonable similarity metric would most likely conclude that a and e are the most similar, so your program should return [('a', 'e')]
.
Click here for solution.
The number 6174
is known as Kaprekar's contant, after the mathematician who discovered an associated property: for all four-digit numbers with at least two distinct digits, repeatedly applying a simple procedure eventually results in this value. The procedure is as follows:
For a given input x
, create two new numbers that consist of the digits in x
in ascending and descending order.
Click here for solution.
Given a set of points (x, y)
on a 2D
cartesian plane, find the two closest points. For example, given the points [(1, 1), (-1, -1), (3, 4), (6, 1), (-1, -6), (-4, -3)]
, return [(-1, -1), (1, 1)]
.
Click here for solution.
You are given an N by N matrix of random letters and a dictionary of words. Find the maximum number of words that can be packed on the board from the given dictionary.
{ 'eat', 'rain', 'in', 'rat' }
and matrix:
[['e', 'a', 'n'],
['t', 't', 'i'],
['a', 'r', 'a']]
Your function should return 3
, since we can make the words 'eat', 'in', and 'rat'
without them touching each other. We could have alternatively made 'eat'
and 'rain'
, but that would be incorrect since that's only 2
words.
Click here for solution.
Click here for this question.
Word sense disambiguation is the problem of determining which sense a word takes on in a particular setting, if that word has multiple meanings. For example, in the sentence "I went to get money from the bank", bank probably means the place where people deposit money, not the land beside a river or lake.
{
"word_1": ["meaning one", "meaning two", ...],
...
"word_n": ["meaning one", "meaning two", ...]
}
Given a sentence, most of whose words are contained in the meaning list above, create an algorithm that determines the likely sense of each possibly ambiguous word.
Click here for solution.
A strobogrammatic
number is a positive number that appears the same after being rotated 180
degrees. For example, 16891
is strobogrammatic.
Click here for solution.
Click here for solution.
The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method.
Click here for solution.
Given k sorted singly linked lists, write a function to merge all the lists into one sorted singly linked list.
Click here for solution.
Click here for solution.
Click here for solution.
Click here for solution.
cons(a, b)
constructs a pair, and car(pair)
and cdr(pair)
returns the first and last element of that pair. For example, car(cons(3, 4))
returns 3
, and cdr(cons(3, 4))
returns 4
.
Click here for solution.
Click here for solution.
Click here for solution.
Given a list of words, determine whether the words can be chained to form a circle. A word X
can be placed in front of another word Y
in a circle if the last character of X
is same as the first character of Y
.
For example, the words ['chair', 'height', 'racket', touch', 'tunic']
can form the following circle: chair --> racket --> touch --> height --> tunic --> chair
Click here for solution.
Click here for solution.
Starting from 0
on a number line, you would like to make a series of jumps that lead to the integer N
.
Click here for solution.
10
/ \
5 5
\ \
2 1
/
-1