1. Imagine you are building a compiler. Before running
any code, the compiler must check that the parentheses in the program are
balanced. Every opening bracket must have a corresponding closing bracket. We
can approximate this using strings.
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Note that an empty string is also considered valid.
Example:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Note that an empty string is also considered valid.
Example:
Input: "((()))"
Output: True
Input: "[()]{}"
Output: True
Input: "({[)]"
Output: False
Output: True
Input: "[()]{}"
Output: True
Input: "({[)]"
Output: False
2. Given a list of numbers, find if there exists
a pythagorean triplet in that list. A pythagorean triplet is 3 variables a, b, c where a^2 + b^2 = c^2
Example:
Example:
Input: [3, 5, 12, 5, 13]
Output: True
Output: True
Here, 5^2 + 12^2 = 13^2.
3. Given a linked list of integers, remove all
consecutive nodes that sum up to 0.
Example:
Example:
Input: 10 -> 5 -> -3 -> -3 -> 1
-> 4 -> -4
Output: 10
Output: 10
The consecutive nodes 5 -> -3 -> -3 -> 1 sums up to 0 so that sequence should be removed. 4 -> -4 also sums up to 0 too so that sequence should also be removed.
4. You
have a landscape, in which puddles can form. You are given an array of non-negative
integers representing the elevation at each location. Return the amount of
water that would accumulate if it rains.
For example: [0,1,0,2,1,0,1,3,2,1,2,1] should return 6 because 6 units of water can get trapped here.
For example: [0,1,0,2,1,0,1,3,2,1,2,1] should return 6 because 6 units of water can get trapped here.
X
X███XX█X
X█XX█XXXXXX
# [0,1,0,2,1,0,1,3,2,1,2,1]
X███XX█X
X█XX█XXXXXX
# [0,1,0,2,1,0,1,3,2,1,2,1]
5. You are
given a string of parenthesis. Return the minimum number of parenthesis that
would need to be removed in order to make the string valid. "Valid"
means that each open parenthesis has a matching closed parenthesis.
Example:
"()())()"
The following input should return 1.
")("
Example:
"()())()"
The following input should return 1.
")("
6. Design a
simple stack that supports push, pop, top, and retrieving the minimum element
in constant time.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
Note: be sure that pop() and top() handle being called on an empty stack.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
Note: be sure that pop() and top() handle being called on an empty stack.
7. Given a number of integers, combine them so it would
create the largest number.
Example:
Example:
Input:
[17, 7, 2, 45, 72]
Output: 77245217
Output: 77245217
8. Given a linked list, determine if the linked
list has a cycle in it. For notation purposes, we use an integer pos which represents the zero-indexed position where
the tail connects to.
Example:
Example:
Input: head = [4,3,2,1,0], pos = 1.
Output: true
Output: true
The example indicates a list where the tail connects to the
second node.
9. By the way, check out our NEW project AlgoPro for over 60+ video coding sessions with
ex-Google/ex-Facebook engineers.
You are given a list of n numbers, where every number is at most k indexes away from its properly sorted index. Write a sorting algorithm (that will be given the number k) for this list that can solve this in O(n log k)
Example:
You are given a list of n numbers, where every number is at most k indexes away from its properly sorted index. Write a sorting algorithm (that will be given the number k) for this list that can solve this in O(n log k)
Example:
Input: [3, 2, 6, 5, 4], k=2
Output: [2, 3, 4, 5, 6]
Output: [2, 3, 4, 5, 6]
As seen above, every number is at most 2 indexes away from
its proper sorted index.
10. Given a postorder
traversal for a binary search tree, reconstruct the tree. A postorder traversal
is a traversal order where the left child always comes before the right child,
and the right child always comes before the parent for all nodes.
11. Given a list of
possible coins in cents, and an amount (in cents) n, return the minimum number of
coins needed to create the amount n.
If it is not possible to create the amount using the given coin denomination,
return None.
12. Given a string s and a character c, find the distance for all characters in the string
to the character c in the
string s. You can assume that the
character c will appear at
least once in the string.
13. Given a square 2D
matrix (n x n), rotate the matrix by 90
degrees clockwise.
14. Given a binary tree,
find all duplicate subtrees (subtrees with the same value and same structure)
and return them as a list of list [subtree1,
subtree2, ...] where subtree1 is a duplicate of subtree2 etc.
15. Given a string s and a character c, find the distance for all characters in the string
to the character c in the
string s. You can assume that the
character c will appear at
least once in the string.
16. Given a binary tree,
and a target number, find if there is a path from the root to any leaf that
sums up to the target.
17. Given a binary tree,
find all duplicate subtrees (subtrees with the same value and same structure)
and return them as a list of list [subtree1,
subtree2, ...] where subtree1 is a duplicate of subtree2 etc.

0 Comments
Please do not Enter any spam link in the comment box