-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterviewQuestions.java
More file actions
208 lines (169 loc) · 4.44 KB
/
Copy pathInterviewQuestions.java
File metadata and controls
208 lines (169 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Solutions By Omari Powell
ojp3@cornell.edu
Done for Tech Draft Application in 2014 to upload to github
*/
import java.util.ArrayList;
import java.util.HashSet;
public class InterviewQuestions {
//Print all permutation of String both iterative and Recursive way?
public String [] permutations(String s ) {
if (s.length()<=1){
return (new String[]{s});
}
// get all permutations of length N-1
String [] perms=permutations(s.substring(1));
//get the character you left out
String curr = s.charAt(0) + "";
ArrayList<String> result = new ArrayList<String>();
//iterate over all permutations of length N-1
for( String str : perms ){
//add the curr character (curr ) to every location in str
for ( int x = 0; x< str.length();x++){
result.add( str.substring(0, x) + curr + s.substring(x));
}
}
return (String[]) result.toArray();
}
/*
* Print the union of two String arrays in O (n+m ) time
*
*
*/
public String[] union( String [] array1, String [] array2){
HashSet<String> seen= new HashSet<String>();
ArrayList<String> list= new ArrayList<String>();
for ( String s : array1){
if ( !seen.add(s) == true){
System.out.println(s);
list.add(s);
};
}
for ( String s : array2){
if ( !seen.add(s) == true){
System.out.println(s);
list.add(s);
};
}
return (String [])list.toArray();
}
/*
* How to tell if a number is to the power of 2?
*/
public boolean isPower2( int n){
if( n< 1 ){
return false;
}
//any numberer to the power of 2 bitwise is this 0100.
//only has one one. so minus one and anding it will be all zeros
return ( (n-1&n) == 0);
}
/*
* How do you find second highest number in an integer array?
*
*
*/
public int secondHighest( int [] array){
int max = Integer.MIN_VALUE;
int secMax= max;
//keep track of the second highest
for( int x : array){
if( x >=max ) {
secMax= max;
max =x;
}
else{
if( x>=secMax){
secMax = x;
}
}
}
return secMax;
}
/*
* Given two arrays, 1,2,3,4,5 and 2,3,1,0,5 find which number is not present in the second array.
*
* returns null if nothing found
*/
public Integer difference(int [] array1, int[] array2){
HashSet<Integer> seen = new HashSet<Integer>();
for( int x : array1){
seen.add(x );
}
for( int x : array2){
if ( !seen.contains(x)){
return x;
}
}
return null;
}
/*
* In an array 1-N numbers are stored randomly in an array (no duplicates)
* .One number is missing how do you find it?
*
*/
public int findIt(int[] array){
//n(n+1)/2
int sum = (array.length)* (array.length-1)/2;
int arraySum = 0;
for( int x : array){
arraySum+=x;
}
return sum - arraySum;
}
/*
*
* How can you tell if a String is a palindrome or not ?
* ere is the detail of parameters: fo substring
beginIndex -- the begin index, inclusive.
endIndex -- the end index, exclusive.
*/
public boolean isPalindrome(String s){
if (s.length() <2){
return true;
}
//are the last characters the same or nah
return s.charAt(0) == s.charAt(s.charAt(s.length() - 1)) ?
isPalindrome( s.substring(1, s.length()-1)) : false;
}
/*
* Write a function to find out longest palindrome in a given string
*/
public int longestPal ( String s){
//this is actually pretty easy
return longestPalHelper( s, 0,0);
}
public int longestPalHelper( String s, int max, int currNum){
if (s.length() <2){
return max;
}
if( s.charAt(0)== s.charAt(s.length()-1)){
//its still a palindrome
currNum++;
if( currNum>max ){
max=currNum;
}
return longestPalHelper(s.substring(1, s.length()-1), max, currNum);
}
//its no loneger a pal currently
return longestPalHelper(s.substring(1, s.length()-1), max, 0);
}
/*
* Write a method which will remove any given character from a String
*
*/
//Remember you cannot cast from a character to a string like so (String )c however you can do this c+"" which is easier
public String removeChar ( String s, char c ){
return s.replace(String.valueOf(c), "");
}
//How to tell if a number is the power of 2 ?
public boolean isPowerTwo( int n){
if( n< 1 ){
return false;
}
//basically in binary form a power of two only as one "1". So in binary n-1 wl be all ones
//but not the shifted over right by 1. So anding it should always be zero.
//any false positives?
return ( ((n-1)&n) == 0);
}
}