Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/kdn251/interviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed Sep 2, 2018
2 parents ea41dee + c6a692b commit 98cd341
Show file tree
Hide file tree
Showing 44 changed files with 1,273 additions and 241 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
pointing to the next node by means of a pointer. It is a data structure
consisting of a group of nodes which together represent a sequence.
* **Singly-linked list**: linked list in which each node points to the next node and the last node points to null
* **Doubly-linked list**: linked list in which each node has two pointers p, n such that p points to the previous node and n points to the next node; the last node's n pointer points to null
* **Doubly-linked list**: linked list in which each node has two pointers, p and n, such that p points to the previous node and n points to the next node; the last node's n pointer points to null
* **Circular-linked list**: linked list in which each node points to the next node and the last node points back to the first node
* Time Complexity:
* Access: `O(n)`
Expand All @@ -72,7 +72,7 @@
### Stack
* A *Stack* is a collection of elements, with two principle operations: *push*, which adds to the collection, and
*pop*, which removes the most recently added element
* Last in, first out data structure (LIFO)
* **Last in, first out data structure (LIFO)**: the most recently added object is the first to be removed
* Time Complexity:
* Access: `O(n)`
* Search: `O(n)`
Expand All @@ -82,7 +82,7 @@
### Queue
* A *Queue* is a collection of elements, supporting two principle operations: *enqueue*, which inserts an element
into the queue, and *dequeue*, which removes an element from the queue
* First in, first out data structure (FIFO)
* **First in, first out data structure (FIFO)**: the oldest added object is the first to be removed
* Time Complexity:
* Access: `O(n)`
* Search: `O(n)`
Expand Down
Binary file modified company/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions company/amazon/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl
//and it returns a short URL such as http://tinyurl.com/4e9iAk.
//
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL
//and the tiny URL can be decoded to the original URL.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
34 changes: 34 additions & 0 deletions company/amazon/FirstUniqueCharacterInAString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
//
//Examples:
//
//s = "leetcode"
//return 0.
//
//s = "loveleetcode",
//return 2.
//Note: You may assume the string contain only lowercase letters.

class FirstUniqueCharacterInAString {
public int firstUniqChar(String s) {
HashMap<Character, Integer> characters = new HashMap<Character, Integer>();
for(int i = 0; i < s.length(); i++) {
char current = s.charAt(i);
if(characters.containsKey(current)) {
characters.put(current, -1);
} else {
characters.put(current, i);
}
}

int min = Integer.MAX_VALUE;
for(char c: characters.keySet()) {
if(characters.get(c) > -1 && characters.get(c) < min) {
min = characters.get(c);
}
}

return min == Integer.MAX_VALUE ? -1 : min;

}
}
34 changes: 34 additions & 0 deletions company/bloomberg/FirstUniqueCharacterInAString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
//
//Examples:
//
//s = "leetcode"
//return 0.
//
//s = "loveleetcode",
//return 2.
//Note: You may assume the string contain only lowercase letters.

class FirstUniqueCharacterInAString {
public int firstUniqChar(String s) {
HashMap<Character, Integer> characters = new HashMap<Character, Integer>();
for(int i = 0; i < s.length(); i++) {
char current = s.charAt(i);
if(characters.containsKey(current)) {
characters.put(current, -1);
} else {
characters.put(current, i);
}
}

int min = Integer.MAX_VALUE;
for(char c: characters.keySet()) {
if(characters.get(c) > -1 && characters.get(c) < min) {
min = characters.get(c);
}
}

return min == Integer.MAX_VALUE ? -1 : min;

}
}
41 changes: 41 additions & 0 deletions company/facebook/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl
//and it returns a short URL such as http://tinyurl.com/4e9iAk.
//
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL
//and the tiny URL can be decoded to the original URL.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
35 changes: 20 additions & 15 deletions company/facebook/LongestConsecutiveSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@

// Your algorithm should run in O(n) complexity.

public class LongestConsecutiveSequence {
class LongestConsecutiveSequence {
public int longestConsecutive(int[] nums) {
int res = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
if(nums == null || nums.length == 0) {
return 0;
}

Set<Integer> set = new HashSet<Integer>();
for(int n: nums) {
set.add(n);
}

for(int n : nums) {
if(!map.containsKey(n)) {
int left = (map.containsKey(n - 1)) ? map.get(n - 1) : 0;
int right = (map.containsKey(n + 1)) ? map.get(n + 1) : 0;
int maxLength = 0;
for(int n: set) {
if(!set.contains(n - 1)) {
int current = n;
int currentMax = 1;

int sum = left + right + 1;
map.put(n, sum);
res = Math.max(res, sum);
while(set.contains(n + 1)) {
currentMax++;
n++;
}

map.put(n - left, sum);
map.put(n + right, sum);
} else {
continue;
maxLength = Math.max(maxLength, currentMax);
}
}

return res;
return maxLength;
}
}
38 changes: 16 additions & 22 deletions company/facebook/MergeIntervals.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class MergeIntervals {
class MergeIntervals {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size() <= 1) {
return intervals;
List<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0) {
return result;
}

// Sort by ascending starting point using an anonymous Comparator
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
Arrays.sort(allIntervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
if(a.start == b.start) {
return a.end - b.end;
}
return a.start - b.start;
}
});

List<Interval> result = new ArrayList<Interval>();

int start = intervals.get(0).start;
int end = intervals.get(0).end;

for(Interval interval : intervals) {
if(interval.start <= end) {
end = Math.max(end, interval.end);
for(Interval i: allIntervals) {
if (result.size() == 0 || result.get(result.size() - 1).end < i.start) {
result.add(i);
} else {
result.add(new Interval(start, end));
start = interval.start;
end = interval.end;
result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, i.end);
}
}

result.add(new Interval(start, end));

return result;
}
}
43 changes: 43 additions & 0 deletions company/google/BullsAndCows.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.
//
//Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows.
//
//Please note that both secret number and friend's guess may contain duplicate digits.
//
//Example 1:
//
//Input: secret = "1807", guess = "7810"
//
//Output: "1A3B"
//
//Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.
//Example 2:
//
//Input: secret = "1123", guess = "0111"
//
//Output: "1A1B"
//
//Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.
//Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

class BullsAndCows {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] counts = new int[10];
for(int i = 0; i < secret.length(); i++) {
if(secret.charAt(i) == guess.charAt(i)) {
bulls++;
} else {
if(counts[secret.charAt(i) - '0']++ < 0) {
cows++;
}
if(counts[guess.charAt(i) - '0']-- > 0) {
cows++;
}
}
}

return bulls + "A" + cows + "B";
}
}
21 changes: 21 additions & 0 deletions company/google/DailyTemperatures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
//
//For example, given the list temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
//
//Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

class DailyTemperatures {
public int[] dailyTemperatures(int[] temperatures) {
int[] result = new int[temperatures.length];
Stack<Integer> stack = new Stack<Integer>();
for(int i = 0; i < temperatures.length; i++) {
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
int index = stack.pop();
result[index] = i - index;
}
stack.push(i);
}

return result;
}
}
41 changes: 41 additions & 0 deletions company/google/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl
//and it returns a short URL such as http://tinyurl.com/4e9iAk.
//
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL
//and the tiny URL can be decoded to the original URL.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
Loading

0 comments on commit 98cd341

Please sign in to comment.