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.
started adding UVa & Cracking the Coding Interview
- Loading branch information
Showing
24 changed files
with
1,472 additions
and
8 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
CrackingTheCodingInterview/Chapter1ArraysAndStrings/IsRotation.java
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,17 @@ | ||
// Assume you have a method isSubstring which checks if one word is a isSubstring of another. | ||
// Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only | ||
// one call to isSubstring(e.g., "waterbottle" is a rotation of "erbottlewat"). | ||
|
||
public class IsRotation { | ||
public boolean isRotation(String s1, String s2) { | ||
int len = s1.length(); | ||
/*check that s1 and s2 are equal length and not empty */ | ||
if(len == s2.length() && len > 0) { | ||
/* concatenate s1 and s1 within new buffer */ | ||
String s1s1 = s1 + s1; | ||
return isSubstring(s1s1, s2); | ||
} | ||
|
||
return false; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
CrackingTheCodingInterview/Chapter1ArraysAndStrings/IsUniqueChars.java
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,15 @@ | ||
//Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? | ||
|
||
public class isUniqueChars { | ||
public boolean isUniqueChars(String str) { | ||
int checker = 0; | ||
for(int i = 0; i < str.length(); i++) { | ||
int val = str.charAt(i) - 'a'; | ||
if((checker & (1 << val)) > 0) { | ||
return false; | ||
} | ||
checker |= (1 << val)); | ||
} | ||
return true; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
CrackingTheCodingInterview/Chapter1ArraysAndStrings/NthToLast.java
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,27 @@ | ||
//Implement an algorithm to find the kth to last element of a single linked list | ||
|
||
public class NthToLast { | ||
LinkedListNode nthToLast(LinkedListNode head, int k) { | ||
if(k <= 0) return null; | ||
|
||
LinkedListNode p1 = head; | ||
LinkedListNode p2 = head; | ||
|
||
//move p2 forward k nodes into the list | ||
for(int i = 0; i < k - 1; i++) { | ||
if(p2 == null) return null; //error check | ||
p2 = p2.next; | ||
} | ||
|
||
if(p2 == null) return null; | ||
|
||
/* now, move p1 and p2 at the same speed. When p2 hits the end, | ||
* p1 will be at the right element */ | ||
while(p2.next != null) { | ||
p1 = p1.next; | ||
p2 = p2.next; | ||
} | ||
|
||
return p1; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
CrackingTheCodingInterview/Chapter1ArraysAndStrings/Permutation.java
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,25 @@ | ||
// Given two strings, write a metho dto decide if one is a permutation of the other | ||
|
||
public class Permutation { | ||
public boolean permutation(String s, String t) { | ||
if(s.length() != t.length()) { | ||
return false; | ||
} | ||
|
||
int[] letters = new int[256]; | ||
|
||
char[] s_array = s.toCharArray(); | ||
for(char c : s_array) { | ||
letters[c]++; | ||
} | ||
|
||
for(int i = 0; i < t.length(); i++) { | ||
int c = (int)t.charAt(i); | ||
if(--letters[c] < 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
CrackingTheCodingInterview/Chapter1ArraysAndStrings/ReplaceSpaces.java
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,30 @@ | ||
// Write a method to replace all spaces in a string with '%20.' You may assum ethat the string | ||
// has sufficient space at th eend of the string to hold the additional characters, and that you | ||
// are given the "true" length of the string. (Note: if implementing in Java, please use a characters | ||
// array so that you can perform this operation in place) | ||
|
||
public class ReplaceSpaces { | ||
public void replaceSpaces(char[] str, int length) { | ||
int spaceCount = 0, newLength; i; | ||
for(int i = 0; i < length; i++) { | ||
if(str[i] == ' ') { | ||
spaceCount++; | ||
} | ||
} | ||
|
||
newLength = length + spaceCount * 2; | ||
str[newLength] = '\0'; | ||
for(int i = length - 1; i >= 0; i--) { | ||
if(str[i] == ' ') { | ||
str[newLength - 1] = '0'; | ||
str[newLength - 2] = '2'; | ||
str[newLength - 3] = '%'; | ||
newLength = newLength - 3; | ||
} | ||
else { | ||
str[newLength - 1] = str[i]; | ||
newLength = newLength - 1; | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
CrackingTheCodingInterview/Chapter2LinkedLists/DeleteDups.java
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,18 @@ | ||
//Write code to remove duplicates from an unsorted linked list | ||
|
||
public class RemoveDups { | ||
void deleteDups(LinkedListNode n) { | ||
HashSet<Integer> set = new HashSet<Integer>(); | ||
LinkedListNode previous = null; | ||
while(n != null) { | ||
if(set.contains(n.data)) { | ||
previous.next = n.next; | ||
} | ||
else { | ||
set.add(n.data); | ||
previous = n; | ||
} | ||
n = n.next; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
CrackingTheCodingInterview/Chapter2LinkedLists/DeleteNode.java
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,14 @@ | ||
//Implement an algorithm to delete a node in the middle of a singly linked list, give only access to that node | ||
|
||
public class DeleteNode { | ||
public static boolean deleteNode(LinkedListNode n) { | ||
if(n == null || n.next == null) { | ||
return false; | ||
} | ||
|
||
LinkedListNode next = n.next; | ||
n.data = next.data; | ||
n.next = next.next; | ||
return true; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
CrackingTheCodingInterview/Chapter2LinkedLists/FindBeginning.java
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,36 @@ | ||
//Given a circular linked list, implement an algorithm which returns | ||
//the node at the beginning of the loop | ||
|
||
public class FindBeginning { | ||
LinkedListNode findBeginning(LinkedListNode head) { | ||
LinkedListNode slow = head; | ||
LinkedListNode fast = head; | ||
|
||
/* find meeting point. This will be LOOP_SIZE - k | ||
* steps int othe linked list */ | ||
while(fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
if(fast == slow) { | ||
break; | ||
} | ||
} | ||
|
||
/* error checking - no meeting point, and therefore no loop */ | ||
if(fast == null || fast.next == null) { | ||
return null; | ||
} | ||
|
||
/* move slow to head. Keep fast at meeting point. Each are k | ||
* steps from the loop start. If they move at the same pace, | ||
* they must meet at the loop start */ | ||
slow = head; | ||
while(slow != fast) { | ||
slow = slow.next; | ||
fast = fast.next; | ||
} | ||
|
||
/* both now point to the start of the loop */ | ||
return fast; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
CrackingTheCodingInterview/Chapter2LinkedLists/IsPalindrome.java
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,38 @@ | ||
//Implement a function to check if a linked list is a palindrome | ||
//don't forget import statements! | ||
|
||
public class IsPalindrome { | ||
boolean isPalindrome(LinkedListNode head) { | ||
LinkedListNode fast = head; | ||
LinkedListNode slow = head; | ||
|
||
Stack<Integer> stack = new Stack<Integer>(); | ||
|
||
/* push elements from first half of linked list onto stack. | ||
* When fast runner (which is moving at 2x speed) reaches the | ||
* end of the linked list, then we know we're at the middle */ | ||
while(fast != null && fast.next != null) { | ||
stack.push(slow.data); | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
/*has odd number of elements, so skip the middle element */ | ||
if(fast != null) { | ||
slow = slow.next; | ||
} | ||
|
||
while(slow != null) { | ||
int top = stack.pop().intValue(); | ||
|
||
/* if values are different, then it's not a palindrome */ | ||
if(top != slow.data) { | ||
return false; | ||
} | ||
|
||
slow = slow.next; | ||
} | ||
|
||
return true; | ||
} | ||
} |
Empty file.
28 changes: 28 additions & 0 deletions
28
CrackingTheCodingInterview/Chapter2LinkedLists/Partition.java
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,28 @@ | ||
//Write code to partition a linked list around a value x, such that | ||
//all nodes less than x come before all nodes greater than or equal to x. | ||
|
||
public class Partition { | ||
LinkedListNode partition(LinkedListNode node, int x) { | ||
LinkedListNode head = node; | ||
LinkedListNode tail = node; | ||
|
||
while(node != null) { | ||
LinkedListNode next = node.next; | ||
if(node.data < x) { | ||
/* insert node at head */ | ||
node.next = head; | ||
head = node; | ||
} | ||
else { | ||
/* insert node at tail */ | ||
tail.next = node; | ||
tail = node; | ||
} | ||
node = next; | ||
} | ||
tail.next = null; | ||
|
||
//the head has changed, so we need to return it to the user | ||
return head; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.