forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
561 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. | ||
|
||
// Formally the function should: | ||
// Return true if there exists i, j, k | ||
// such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false. | ||
// Your algorithm should run in O(n) time complexity and O(1) space complexity. | ||
|
||
// Examples: | ||
// Given [1, 2, 3, 4, 5], | ||
// return true. | ||
|
||
// Given [5, 4, 3, 2, 1], | ||
// return false. | ||
|
||
public class Solution { | ||
|
||
public boolean increasingTriplet(int[] nums) { | ||
|
||
int firstMin = Integer.MAX_VALUE; | ||
int secondMin = Integer.MAX_VALUE; | ||
|
||
for(int n : nums) { | ||
|
||
if(n <= firstMin) firstMin = n; | ||
else if(n < secondMin) secondMin = n; | ||
else if(n > secondMin) return true; | ||
|
||
} | ||
|
||
return false; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. | ||
|
||
// Solve it without division and in O(n). | ||
|
||
// For example, given [1,2,3,4], return [24,12,8,6]. | ||
|
||
// Follow up: | ||
// Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) | ||
|
||
public class Solution { | ||
|
||
public int[] productExceptSelf(int[] nums) { | ||
|
||
int n = nums.length; | ||
int[] result = new int[n]; | ||
int left = 1; | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
|
||
if(i > 0) { | ||
|
||
left *= nums[i - 1]; | ||
|
||
} | ||
|
||
result[i] = left; | ||
|
||
} | ||
|
||
int right = 1; | ||
|
||
for(int i = n - 1; i >= 0; i--) { | ||
|
||
if(i < n - 1) { | ||
|
||
right *= nums[i + 1]; | ||
|
||
} | ||
|
||
result[i] *= right; | ||
|
||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: | ||
|
||
// You receive a valid board, made of only battleships or empty slots. | ||
// Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size. | ||
// At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships. | ||
|
||
// Example: | ||
// X..X | ||
// ...X | ||
// ...X | ||
// In the above board there are 2 battleships. | ||
|
||
// Invalid Example: | ||
// ...X | ||
// XXXX | ||
// ...X | ||
// This is an invalid board that you will not receive - as battleships will always have a cell separating between them. | ||
|
||
// Follow up: | ||
// Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board? | ||
|
||
public class Solution { | ||
|
||
public int countBattleships(char[][] board) { | ||
|
||
int ships = 0; | ||
|
||
for(int i = 0; i < board.length; i++) { | ||
|
||
for(int j = 0; j < board[0].length; j++) { | ||
|
||
if(board[i][j] == 'X') { | ||
|
||
ships++; | ||
sink(board, i, j, 1); | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
return ships; | ||
|
||
} | ||
|
||
public void sink(char[][] board, int i, int j, int numberOfShips) { | ||
|
||
if(i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] == '.') { | ||
|
||
return; | ||
|
||
} | ||
|
||
board[i][j] = '.'; | ||
sink(board, i + 1, j, numberOfShips + 1); | ||
sink(board, i, j + 1, numberOfShips + 1); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Given two 1d vectors, implement an iterator to return their elements alternately. | ||
|
||
// For example, given two 1d vectors: | ||
|
||
// v1 = [1, 2] | ||
// v2 = [3, 4, 5, 6] | ||
// By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6]. | ||
|
||
// Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases? | ||
|
||
/** | ||
* Your ZigzagIterator object will be instantiated and called as such: | ||
* ZigzagIterator i = new ZigzagIterator(v1, v2); | ||
* while (i.hasNext()) v[f()] = i.next(); | ||
*/ | ||
|
||
public class ZigzagIterator { | ||
|
||
private Iterator<Integer> i; | ||
private Iterator<Integer> j; | ||
private Iterator<Integer> temp; | ||
|
||
public ZigzagIterator(List<Integer> v1, List<Integer> v2) { | ||
|
||
i = v1.iterator(); | ||
j = v2.iterator(); | ||
|
||
} | ||
|
||
public int next() { | ||
|
||
if(i.hasNext()) { | ||
|
||
temp = i; | ||
i = j; | ||
j = temp; | ||
|
||
} | ||
|
||
return j.next(); | ||
|
||
|
||
} | ||
|
||
public boolean hasNext() { | ||
|
||
return i.hasNext() || j.hasNext(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: | ||
|
||
// "abc" -> "bcd" -> ... -> "xyz" | ||
// Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence. | ||
|
||
// For example, given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], | ||
// A solution is: | ||
|
||
// [ | ||
// ["abc","bcd","xyz"], | ||
// ["az","ba"], | ||
// ["acef"], | ||
// ["a","z"] | ||
// ] | ||
|
||
public class Solution { | ||
|
||
public List<List<String>> groupStrings(String[] strings) { | ||
|
||
List<List<String>> result = new ArrayList<List<String>>(); | ||
|
||
HashMap<String, List<String>> map = new HashMap<String, List<String>>(); | ||
|
||
for(String s : strings) { | ||
|
||
int offset = s.charAt(0) - 'a'; | ||
String key = ""; | ||
|
||
for(int i = 0; i < s.length(); i++) { | ||
|
||
char current = (char)(s.charAt(i) - offset); | ||
|
||
if(current < 'a') { | ||
|
||
current += 26; | ||
|
||
} | ||
|
||
key += current; | ||
|
||
} | ||
|
||
if(!map.containsKey(key)) { | ||
|
||
List<String> list = new ArrayList<String>(); | ||
map.put(key, list); | ||
|
||
} | ||
|
||
map.get(key).add(s); | ||
|
||
} | ||
|
||
for(String key : map.keySet()) { | ||
|
||
List<String> list = map.get(key); | ||
|
||
Collections.sort(list); | ||
|
||
result.add(list); | ||
|
||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Given a binary tree, find the maximum path sum. | ||
|
||
// For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. | ||
|
||
// For example: | ||
// Given the below binary tree, | ||
|
||
// 1 | ||
// / \ | ||
// 2 3 | ||
// Return 6. | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public class Solution { | ||
|
||
int max = Integer.MIN_VALUE; | ||
|
||
public int maxPathSum(TreeNode root) { | ||
|
||
maxPathSumRecursive(root); | ||
return max; | ||
|
||
} | ||
|
||
private int maxPathSumRecursive(TreeNode root) { | ||
|
||
if(root == null) return 0; | ||
|
||
int left = Math.max(maxPathSumRecursive(root.left), 0); | ||
int right = Math.max(maxPathSumRecursive(root.right), 0); | ||
|
||
max = Math.max(max, root.val + left + right); | ||
|
||
return root.val + Math.max(left, right); | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Given a binary tree, return all root-to-leaf paths. | ||
|
||
// For example, given the following binary tree: | ||
|
||
// 1 | ||
// / \ | ||
// 2 3 | ||
// \ | ||
// 5 | ||
// All root-to-leaf paths are: | ||
|
||
// ["1->2->5", "1->3"] | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public class Solution { | ||
|
||
public List<String> binaryTreePaths(TreeNode root) { | ||
|
||
List<String> result = new ArrayList<String>(); | ||
|
||
if(root == null) return result; | ||
|
||
helper(new String(), root, result); | ||
|
||
return result; | ||
|
||
} | ||
|
||
public void helper(String current, TreeNode root, List<String> result) { | ||
|
||
if(root.left == null && root.right == null) result.add(current + root.val); | ||
if(root.left != null) helper(current + root.val + "->", root.left, result); | ||
if(root.right != null) helper(current + root.val + "->", root.right, result); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.