Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformat files #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions company/amazon/ThreeSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

// Note: The solution set must not contain duplicate triplets.

// For example, given array S = [-1, 0, 1, 2, -1, -4],

// A solution set is:
// [
// [-1, 0, 1],
// [-1, -1, 2]
// ]

public class ThreeSum {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; i++) {
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}

int j = i + 1;
int k = nums.length - 1;
int target = -nums[i];

while(j < k) {
if(nums[j] + nums[k] == target) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[k]);

result.add(temp);

j++;
k--;

while(j < k && nums[j] == nums[j - 1]) j++;
while(j < k && nums[k] == nums[k + 1]) k--;
}
else if(nums[j] + nums[k] > target) {
k--;
}
else {
j++;
}
}
}

return result;
}
}
20 changes: 20 additions & 0 deletions company/facebook/PowerOfXToTheN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Implement pow(x, n).

public class PowerOfXToTheN {
public double myPow(double x, int n) {
if(n == 0) {
return 1;
}

if(Double.isInfinite(x)) {
return 0;
}

if(n < 0) {
n = -n;
x = 1 / x;
}

return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
}
}
28 changes: 28 additions & 0 deletions company/facebook/SquareRootX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Implement int sqrt(int x).

// Compute and return the square root of x.

public class SquareRootX {
public int mySqrt(int x) {
if(x == 0) {
return 0;
}

int left = 1;
int right = x;

while(left <= right) {
int mid = left + (right - left) / 2;

if(mid == x / mid) {
return mid;
} else if(mid > x / mid) {
right = mid - 1;
} else if(mid < x / mid) {
left = mid + 1;
}
}

return right;
}
}
60 changes: 60 additions & 0 deletions company/google/LoggerRateLimit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.

// Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.

// It is possible that several messages arrive roughly at the same time.

// Example:

// Logger logger = new Logger();

// // logging string "foo" at timestamp 1
// logger.shouldPrintMessage(1, "foo"); returns true;

// // logging string "bar" at timestamp 2
// logger.shouldPrintMessage(2,"bar"); returns true;

// // logging string "foo" at timestamp 3
// logger.shouldPrintMessage(3,"foo"); returns false;

// // logging string "bar" at timestamp 8
// logger.shouldPrintMessage(8,"bar"); returns false;

// // logging string "foo" at timestamp 10
// logger.shouldPrintMessage(10,"foo"); returns false;

// // logging string "foo" at timestamp 11
// logger.shouldPrintMessage(11,"foo"); returns true;

public class LoggerRateLimit {
HashMap<String, Integer> messages;

/** Initialize your data structure here. */
public Logger() {
this.messages = new HashMap<String, Integer>();
}

/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
If this method returns false, the message will not be printed.
The timestamp is in seconds granularity. */
public boolean shouldPrintMessage(int timestamp, String message) {
if(messages.containsKey(message)) {
if(timestamp - messages.get(message) >= 10) {
messages.put(message, timestamp);

return true;
} else {
return false;
}
} else {
messages.put(message, timestamp);
return true;
}
}
}

/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/
20 changes: 20 additions & 0 deletions company/google/PowerOfXToTheN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Implement pow(x, n).

public class PowerOfXToTheN {
public double myPow(double x, int n) {
if(n == 0) {
return 1;
}

if(Double.isInfinite(x)) {
return 0;
}

if(n < 0) {
n = -n;
x = 1 / x;
}

return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
}
}
59 changes: 59 additions & 0 deletions company/google/Utf8Validation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

// For 1-byte character, the first bit is a 0, followed by its unicode code.
// For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
// This is how the UTF-8 encoding would work:

// Char. number range | UTF-8 octet sequence
// (hexadecimal) | (binary)
// --------------------+---------------------------------------------
// 0000 0000-0000 007F | 0xxxxxxx
// 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
// 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
// 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
// Given an array of integers representing the data, return whether it is a valid utf-8 encoding.

// Note:
// The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.

// Example 1:

// data = [197, 130, 1], which represents the octet sequence: 11000101 10000010 00000001.

// Return true.
// It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
// Example 2:

// data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100.

// Return false.
// The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
// The next byte is a continuation byte which starts with 10 and that's correct.
// But the second continuation byte does not start with 10, so it is invalid.

public class Utf8Validation {
public boolean validUtf8(int[] data) {
int count = 0;
for(int i : data) {
if(count == 0) {
if((i >> 5) == 0b110) {
count = 1;
} else if((i >> 4) == 0b1110) {
count = 2;
} else if((i >> 3) == 0b11110) {
count = 3;
} else if((i >> 7) == 0b1) {
return false;
}
} else {
if((i >> 6) != 0b10) {
return false;
}

count--;
}
}

return count == 0;
}
}
20 changes: 20 additions & 0 deletions company/linkedin/PowerOfXToTheN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Implement pow(x, n).

public class PowerOfXToTheN {
public double myPow(double x, int n) {
if(n == 0) {
return 1;
}

if(Double.isInfinite(x)) {
return 0;
}

if(n < 0) {
n = -n;
x = 1 / x;
}

return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
}
}
23 changes: 23 additions & 0 deletions company/uber/PadlindromePermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class PalindromePermutation {
public boolean canPermutePalindrome(String s) {
char[] characters = new char[256];

for(int i = 0; i < s.length(); i++) {
characters[s.charAt(i)]++;
}

int oddCount = 0;

for(int i = 0; i < characters.length; i++) {
if(!(characters[i] % 2 == 0)) {
oddCount++;

if(oddCount > 1) {
return false;
}
}
}

return true;
}
}
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;
}
}
}
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;
}
}
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;
}
}
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;
}
}
Loading