Skip to content

Commit 93db90d

Browse files
committedDec 24, 2018
Refactored the project
0 parents  commit 93db90d

6 files changed

+174
-0
lines changed
 

‎src/Main.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Main {
2+
3+
public static void main(String[] args) {
4+
System.out.println("Hello World!");
5+
}
6+
}

‎src/google/AddToInteger.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package google;
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
6+
//This problem was recently asked by Google.
7+
//
8+
//Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
9+
//
10+
//For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
11+
//
12+
//Bonus: Can you do this in one pass?
13+
public class AddToInteger {
14+
public static void main(String[] args){
15+
AddToInteger instance = new AddToInteger();
16+
int sum = 13;
17+
int[] array = {10, 15, 3, 7};
18+
System.out.println(instance.willAddToInteger(sum, array));
19+
}
20+
public boolean willAddToInteger(int sum, int[] array){
21+
HashMap<Integer,Boolean> temp = new HashMap<Integer,Boolean>();
22+
int i = 0;
23+
boolean result = false;
24+
25+
while(i < array.length){
26+
try{
27+
int currentElement = array[i];
28+
int pair = sum - currentElement;
29+
30+
if(temp.containsKey(pair)){
31+
result = true;
32+
}else{
33+
temp.put(currentElement, true);
34+
}
35+
i++;
36+
}catch (Exception e){
37+
System.out.println(e);
38+
}
39+
40+
}
41+
42+
return result;
43+
}
44+
}

‎src/google/BinarySerialize.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package google;
2+
3+
/*
4+
This problem was asked by Google.
5+
6+
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
7+
8+
For example, given the following Node class
9+
10+
class Node:
11+
def __init__(self, val, left=None, right=None):
12+
self.val = val
13+
self.left = left
14+
self.right = right
15+
The following test should pass:
16+
17+
node = Node('root', Node('left', Node('left.left')), Node('right'))
18+
assert deserialize(serialize(node)).left.left.val == 'left.left'
19+
20+
*/
21+
public class BinarySerialize {
22+
public static void main(String[] args){
23+
24+
}
25+
26+
}

‎src/google/Fibonacci.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.company.com.company.google;
2+
3+
public class Fibonacci {
4+
public Fibonacci(){
5+
6+
}
7+
public static void main(String[] args){
8+
Fibonacci instance = new Fibonacci();
9+
System.out.println(instance.getFibonacci(100));
10+
}
11+
12+
public int getFibonacci(int n){
13+
int result;
14+
int memo[] = new int[n+1]; // memoize array
15+
memo[0] = 1; //handle base case
16+
memo[1] =1;
17+
//back-track memoize array in O(n)
18+
for(int i=2; i <= n ;i++){
19+
memo[i] = memo[i-1] + memo[i-2];
20+
}
21+
return memo[n];
22+
}
23+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package google;
2+
3+
/*
4+
This problem was asked by Stripe.
5+
6+
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
7+
8+
For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
9+
10+
You can modify the input array in-place.
11+
*/
12+
public class FirstMissingPositiveInt {
13+
public static void main(String[] args){
14+
int[] arr = {1, 2, 0};
15+
FirstMissingPositiveInt instance = new FirstMissingPositiveInt();
16+
System.out.println(instance.getFirstMissing(arr));
17+
}
18+
public int getFirstMissing(int[] arr){
19+
int n = arr.length;
20+
int min= arr[0] ,i=0;
21+
22+
for(int j=0;j<n;j++){
23+
24+
}
25+
26+
while(i<n){
27+
if(arr[i] < min && arr[i] > 0 ){
28+
min = arr[i];
29+
}
30+
i++;
31+
}
32+
return min;
33+
}
34+
}

‎src/uber/MultiplyAllExceptIndex.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package uber;/*This problem was asked by Uber.
2+
3+
Given an array of integers, return a new array such that each element at index i of the new array
4+
is the product of all the numbers in the original array except the one at i.
5+
6+
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24].
7+
If our input was [3, 2, 1], the expected output would be [2, 3, 6].
8+
9+
Follow-up: what if you can't use division?
10+
*/
11+
12+
import java.util.Arrays;
13+
14+
public class MultiplyAllExceptIndex {
15+
public static void main(String[] args){
16+
MultiplyAllExceptIndex instance = new MultiplyAllExceptIndex();
17+
int[] arr = {1, 2, 3, 4, 5};
18+
System.out.println(instance.getProductArray((arr)));
19+
}
20+
//O(n) solution
21+
public String getProductArray(int[] arr){
22+
int n = arr.length;
23+
int[] left = new int [n];
24+
int[] right = new int [n];
25+
int[] result = new int[n];
26+
left[0] = 1;
27+
right[n-1] = 1;
28+
29+
for(int i=1;i<n;i++){
30+
left[i] = left[i-1]*arr[i-1];
31+
}
32+
for(int j= n-2;j>=0;j--){
33+
right[j] = right[j+1]*arr[j+1];
34+
}
35+
for(int k= 0; k< n; k++){
36+
result[k] = left[k]*right[k];
37+
}
38+
39+
return Arrays.toString(result);
40+
}
41+
}

0 commit comments

Comments
 (0)
Please sign in to comment.