Skip to content

Commit 98cd341

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
Merge branch 'master' of https://github.com/kdn251/interviews
2 parents ea41dee + c6a692b commit 98cd341

44 files changed

Lines changed: 1273 additions & 241 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.DS_Store

0 Bytes
Binary file not shown.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
pointing to the next node by means of a pointer. It is a data structure
6262
consisting of a group of nodes which together represent a sequence.
6363
* **Singly-linked list**: linked list in which each node points to the next node and the last node points to null
64-
* **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
64+
* **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
6565
* **Circular-linked list**: linked list in which each node points to the next node and the last node points back to the first node
6666
* Time Complexity:
6767
* Access: `O(n)`
@@ -72,7 +72,7 @@
7272
### Stack
7373
* A *Stack* is a collection of elements, with two principle operations: *push*, which adds to the collection, and
7474
*pop*, which removes the most recently added element
75-
* Last in, first out data structure (LIFO)
75+
* **Last in, first out data structure (LIFO)**: the most recently added object is the first to be removed
7676
* Time Complexity:
7777
* Access: `O(n)`
7878
* Search: `O(n)`
@@ -82,7 +82,7 @@
8282
### Queue
8383
* A *Queue* is a collection of elements, supporting two principle operations: *enqueue*, which inserts an element
8484
into the queue, and *dequeue*, which removes an element from the queue
85-
* First in, first out data structure (FIFO)
85+
* **First in, first out data structure (FIFO)**: the oldest added object is the first to be removed
8686
* Time Complexity:
8787
* Access: `O(n)`
8888
* Search: `O(n)`

company/.DS_Store

-10 KB
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl
2+
//and it returns a short URL such as http://tinyurl.com/4e9iAk.
3+
//
4+
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your
5+
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL
6+
//and the tiny URL can be decoded to the original URL.
7+
8+
public class EncodeAndDecodeTinyURL {
9+
HashMap<String, String> map = new HashMap<String, String>();
10+
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
11+
int count = 1;
12+
13+
public String getKey() {
14+
String key = "";
15+
while(count > 0) {
16+
count--;
17+
key += characters.charAt(count);
18+
count /= characters.length();
19+
}
20+
21+
return key;
22+
}
23+
24+
// Encodes a URL to a shortened URL.
25+
public String encode(String longUrl) {
26+
String key = getKey();
27+
map.put(key, longUrl);
28+
count++;
29+
30+
return "http://tinyurl.com/" + key;
31+
}
32+
33+
// Decodes a shortened URL to its original URL.
34+
public String decode(String shortUrl) {
35+
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
36+
}
37+
}
38+
39+
// Your Codec object will be instantiated and called as such:
40+
// Codec codec = new Codec();
41+
// codec.decode(codec.encode(url));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
2+
//
3+
//Examples:
4+
//
5+
//s = "leetcode"
6+
//return 0.
7+
//
8+
//s = "loveleetcode",
9+
//return 2.
10+
//Note: You may assume the string contain only lowercase letters.
11+
12+
class FirstUniqueCharacterInAString {
13+
public int firstUniqChar(String s) {
14+
HashMap<Character, Integer> characters = new HashMap<Character, Integer>();
15+
for(int i = 0; i < s.length(); i++) {
16+
char current = s.charAt(i);
17+
if(characters.containsKey(current)) {
18+
characters.put(current, -1);
19+
} else {
20+
characters.put(current, i);
21+
}
22+
}
23+
24+
int min = Integer.MAX_VALUE;
25+
for(char c: characters.keySet()) {
26+
if(characters.get(c) > -1 && characters.get(c) < min) {
27+
min = characters.get(c);
28+
}
29+
}
30+
31+
return min == Integer.MAX_VALUE ? -1 : min;
32+
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
2+
//
3+
//Examples:
4+
//
5+
//s = "leetcode"
6+
//return 0.
7+
//
8+
//s = "loveleetcode",
9+
//return 2.
10+
//Note: You may assume the string contain only lowercase letters.
11+
12+
class FirstUniqueCharacterInAString {
13+
public int firstUniqChar(String s) {
14+
HashMap<Character, Integer> characters = new HashMap<Character, Integer>();
15+
for(int i = 0; i < s.length(); i++) {
16+
char current = s.charAt(i);
17+
if(characters.containsKey(current)) {
18+
characters.put(current, -1);
19+
} else {
20+
characters.put(current, i);
21+
}
22+
}
23+
24+
int min = Integer.MAX_VALUE;
25+
for(char c: characters.keySet()) {
26+
if(characters.get(c) > -1 && characters.get(c) < min) {
27+
min = characters.get(c);
28+
}
29+
}
30+
31+
return min == Integer.MAX_VALUE ? -1 : min;
32+
33+
}
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl
2+
//and it returns a short URL such as http://tinyurl.com/4e9iAk.
3+
//
4+
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your
5+
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL
6+
//and the tiny URL can be decoded to the original URL.
7+
8+
public class EncodeAndDecodeTinyURL {
9+
HashMap<String, String> map = new HashMap<String, String>();
10+
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
11+
int count = 1;
12+
13+
public String getKey() {
14+
String key = "";
15+
while(count > 0) {
16+
count--;
17+
key += characters.charAt(count);
18+
count /= characters.length();
19+
}
20+
21+
return key;
22+
}
23+
24+
// Encodes a URL to a shortened URL.
25+
public String encode(String longUrl) {
26+
String key = getKey();
27+
map.put(key, longUrl);
28+
count++;
29+
30+
return "http://tinyurl.com/" + key;
31+
}
32+
33+
// Decodes a shortened URL to its original URL.
34+
public String decode(String shortUrl) {
35+
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
36+
}
37+
}
38+
39+
// Your Codec object will be instantiated and called as such:
40+
// Codec codec = new Codec();
41+
// codec.decode(codec.encode(url));

company/facebook/LongestConsecutiveSequence.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@
66

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

9-
public class LongestConsecutiveSequence {
9+
class LongestConsecutiveSequence {
1010
public int longestConsecutive(int[] nums) {
11-
int res = 0;
12-
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11+
if(nums == null || nums.length == 0) {
12+
return 0;
13+
}
14+
15+
Set<Integer> set = new HashSet<Integer>();
16+
for(int n: nums) {
17+
set.add(n);
18+
}
1319

14-
for(int n : nums) {
15-
if(!map.containsKey(n)) {
16-
int left = (map.containsKey(n - 1)) ? map.get(n - 1) : 0;
17-
int right = (map.containsKey(n + 1)) ? map.get(n + 1) : 0;
20+
int maxLength = 0;
21+
for(int n: set) {
22+
if(!set.contains(n - 1)) {
23+
int current = n;
24+
int currentMax = 1;
1825

19-
int sum = left + right + 1;
20-
map.put(n, sum);
21-
res = Math.max(res, sum);
26+
while(set.contains(n + 1)) {
27+
currentMax++;
28+
n++;
29+
}
2230

23-
map.put(n - left, sum);
24-
map.put(n + right, sum);
25-
} else {
26-
continue;
31+
maxLength = Math.max(maxLength, currentMax);
2732
}
2833
}
2934

30-
return res;
35+
return maxLength;
3136
}
3237
}

company/facebook/MergeIntervals.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,31 @@
1313
* Interval(int s, int e) { start = s; end = e; }
1414
* }
1515
*/
16-
public class MergeIntervals {
16+
class MergeIntervals {
1717
public List<Interval> merge(List<Interval> intervals) {
18-
if(intervals.size() <= 1) {
19-
return intervals;
18+
List<Interval> result = new ArrayList<Interval>();
19+
if(intervals == null || intervals.size() == 0) {
20+
return result;
2021
}
2122

22-
// Sort by ascending starting point using an anonymous Comparator
23-
Collections.sort(intervals, new Comparator<Interval>() {
24-
@Override
25-
public int compare(Interval i1, Interval i2) {
26-
return Integer.compare(i1.start, i2.start);
27-
}
23+
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
24+
Arrays.sort(allIntervals, new Comparator<Interval>() {
25+
public int compare(Interval a, Interval b) {
26+
if(a.start == b.start) {
27+
return a.end - b.end;
28+
}
29+
return a.start - b.start;
30+
}
2831
});
2932

30-
List<Interval> result = new ArrayList<Interval>();
31-
32-
int start = intervals.get(0).start;
33-
int end = intervals.get(0).end;
34-
35-
for(Interval interval : intervals) {
36-
if(interval.start <= end) {
37-
end = Math.max(end, interval.end);
33+
for(Interval i: allIntervals) {
34+
if (result.size() == 0 || result.get(result.size() - 1).end < i.start) {
35+
result.add(i);
3836
} else {
39-
result.add(new Interval(start, end));
40-
start = interval.start;
41-
end = interval.end;
37+
result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, i.end);
4238
}
4339
}
4440

45-
result.add(new Interval(start, end));
46-
4741
return result;
4842
}
4943
}

company/google/BullsAndCows.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//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.
2+
//
3+
//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.
4+
//
5+
//Please note that both secret number and friend's guess may contain duplicate digits.
6+
//
7+
//Example 1:
8+
//
9+
//Input: secret = "1807", guess = "7810"
10+
//
11+
//Output: "1A3B"
12+
//
13+
//Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.
14+
//Example 2:
15+
//
16+
//Input: secret = "1123", guess = "0111"
17+
//
18+
//Output: "1A1B"
19+
//
20+
//Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.
21+
//Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
22+
23+
class BullsAndCows {
24+
public String getHint(String secret, String guess) {
25+
int bulls = 0;
26+
int cows = 0;
27+
int[] counts = new int[10];
28+
for(int i = 0; i < secret.length(); i++) {
29+
if(secret.charAt(i) == guess.charAt(i)) {
30+
bulls++;
31+
} else {
32+
if(counts[secret.charAt(i) - '0']++ < 0) {
33+
cows++;
34+
}
35+
if(counts[guess.charAt(i) - '0']-- > 0) {
36+
cows++;
37+
}
38+
}
39+
}
40+
41+
return bulls + "A" + cows + "B";
42+
}
43+
}

0 commit comments

Comments
 (0)