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

Add files via upload #3

Open
wants to merge 10 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
46 changes: 46 additions & 0 deletions challenges/April22.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package hanson;
/*Good morning! Here's your coding interview problem for today.

This problem was recently asked by Google.

Given a list of numbers and a number k, return whether any two numbers from the list add up to k.

For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

Bonus: Can you do this in one pass?*/
import java.io.*;
import java.util.HashSet;

public class April22
{



static void printpairs(int arr[],int sum)
{
HashSet<Integer> s = new HashSet<Integer>();
for (int i=0; i<arr.length; ++i)
{
int temp = sum-arr[i];

// checking for condition
if (temp>=0 && s.contains(temp))
{
System.out.println("Pair with given sum " +
sum + " is (" + arr[i] +
", "+temp+")");
}
s.add(arr[i]);
}
}

// Main to test the above function
public static void main (String[] args)
{
int A[] = {10,15,3,7};
int n = 17;
printpairs(A, n);
}


}
48 changes: 48 additions & 0 deletions challenges/ArrayProdExclude_Itself.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package hanson;
/* #02
This problem was asked by Uber.

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].

Follow-up: what if you can't use division? */

//import java.io.*;
/*using division tech , we can solve this in O(n) time.
we cannot use division technique.. */

public class ArrayProdExclude_Itself
{
static int[] res_arr=new int[100];


static void calc(int arr[],int n)
{
int prod=1;
for (int i=0;i<n;i++)
{
prod=1;
for(int j=0;j<n;j++)
{
if(!(i==j))
{
prod*=arr[j];
}

}
res_arr[i]=prod;
}
}

public static void main(String arg[])
{
int[] arry= {2,3,4};
calc(arry,3);
for(int i=0;i<arry.length;i++)
{
System.out.print(res_arr[i]+",");
}
}

}
43 changes: 43 additions & 0 deletions challenges/DCP27braces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handson;

public class DCP27braces
{
char[] stac=new char[10];
int top=-1;
void push(char ch)
{
stac[++top]=ch;
}
char pop()
{
return stac[top--];
}
public static void main(String ag[])
{ char res='a';
int flag=1;
DCP27braces ob=new DCP27braces();
String s="[{[]}]{}";
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='{' || s.charAt(i)=='(' || s.charAt(i)=='[')
ob.push(s.charAt(i));

else if(s.charAt(i)=='}' || s.charAt(i)==')' || s.charAt(i)==']')
{
res=ob.pop();


if ((s.charAt(i)=='}' && res != '{')||( s.charAt(i)==']' && res !='[')||( s.charAt(i)==')'&& res !='('))
{
flag=0;
}
}

}
if( flag ==0)
System.out.println("no");
else
System.out.print("es");
}

}
89 changes: 89 additions & 0 deletions challenges/EvaluationTree_day50.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package handson;


// Class to represent the nodes of syntax tree

class nod
{
nod left;
nod right;
int data;
nod(char value)
{
left = null;
data = value;
right = null;
}
}
public class EvaluationTree_day50
{


// This function receives a node of the syntax tree
// and recursively evaluate it
static nod root;
public static int evaluateExpressionTree(nod root)
{

// empty tree
if (root == null)
return 0;

//leaf node
if( (root.left == null) && (root.right == null) )
{
int val=Character.getNumericValue(root.data);
return val;
}

//evaluate left tree
int left_sum = evaluateExpressionTree(root.left) ;

// evaluate right tree
int right_sum = evaluateExpressionTree(root.right) ;

// check which operation to apply
if (root.data == '+')
return left_sum + right_sum ;

else if( root.data == '-')
return left_sum - right_sum ;

else if (root.data == '*')
return left_sum * right_sum ;

else
return (left_sum / right_sum );
}

// Driver function to test above problem
public static void main(String arg[])
{

//EvalTree e=new EvalTree();
// creating a sample tree
root = new nod('+');
root.left =new nod('*') ;
root.left.left =new nod('5') ;
root.left.right =new nod('4');
root.right = new nod('-') ;
root.right.left =new nod('2') ;
root.right.right =new nod('1');
System.out.println( evaluateExpressionTree(root));

/* root = None

#creating a sample tree
root = node('+')
root.left = node('*')
root.left.left = node('5')
root.left.right = node('4')
root.right = node('-')
root.right.left = node('100')
root.right.right = node('/')
root.right.right.left = node('20')
root.right.right.right = node('2')
print evaluateExpressionTree(root)
*/
}
}
120 changes: 120 additions & 0 deletions challenges/FstMissngPosInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package hanson;
//#04
/*This problem was asked by Stripe.

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.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

You can modify the input array in-place.



A naive method to solve this problem is to search all positive integers, starting from 1 in the given array. We may have to search at most n+1 numbers in the given array. So this solution takes O(n^2) in worst case.

We can use sorting to solve it in lesser time complexity. We can sort the array in O(nLogn) time. Once the array is sorted, then all we need to do is a linear scan of the array. So this approach takes O(nLogn + n) time which is O(nLogn).

We can also use hashing. We can build a hash table of all positive elements in the given array. Once the hash table is built. We can look in the hash table for all positive integers, starting from 1. As soon as we find a number which is not there in hash table, we return it. This approach may take O(n) time on average, but it requires O(n) extra space.



*/


/*
* Following is the two step algorithm.
1) Segregate positive numbers from others i.e., move all non-positive numbers to left side. In the following code, segregate() function does this part.
2) Now we can ignore non-positive elements and consider only the part of array which contains all positive elements.
We traverse the array containing all positive numbers and to mark presence of an element x,
we change the sign of value at index x to negative. We traverse the array again and print the first index which has positive value. In the following code,
findMissingPositive() function does this part. Note that in findMissingPositive, we have subtracted 1 from the values as indexes start from 0 in C.
*/

public class FstMissngPosInt
{



/* Utility function that puts all non-positive
(0 and negative) numbers on left side of
arr[] and return count of such numbers */
static int segregate (int arr[], int size)
{
int j = 0, i;
for(i = 0; i < size; i++)
{
if (arr[i] <= 0)
{
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// increment count of non-positive
// integers
j++;
}
}

return j;
}

/* Find the smallest positive missing
number in an array that contains
all positive integers */
static int findMissingPositive(int arr[], int size)
{
int i;

// Mark arr[i] as visited by making
// arr[arr[i] - 1] negative. Note that
// 1 is subtracted because index start
// from 0 and positive numbers start from 1
for(i = 0; i < size; i++)
{
int x = Math.abs(arr[i]);
if(x - 1 < size && arr[x - 1] > 0)
arr[x - 1] = -arr[x - 1];
}

// Return the first index value at which
// is positive
for(i = 0; i < size; i++)
if (arr[i] > 0)
return i+1; // 1 is added becuase indexes
// start from 0

return size+1;
}

/* Find the smallest positive missing
number in an array that contains
both positive and negative integers */
static int findMissing(int arr[], int size)
{
// First separate positive and
// negative numbers
int shift = segregate (arr, size);
int arr2[] = new int[size-shift];
int j=0;
for(int i=shift;i<size;i++)
{
arr2[j] = arr[i];
j++;
}
// Shift the array and call
// findMissingPositive for
// positive part
return findMissingPositive(arr2, j);
}
// main function
public static void main (String[] args)
{
int arr[] = {0, 10, 2, -10, -20};
int arr_size = arr.length;
int missing = findMissing(arr, arr_size);
System.out.println("The smallest positive missing number is "+
missing);
}
}


Loading