Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
/// missed the minimum of even part, thought max
/// Small Edit
int minEven = 2^40;
int i = 1;
while(i != 0)
{
string line = Console.ReadLine();
int.TryParse(line, out val);
i = val;
if (val != 0)
{
if (checkOdd(val))
{
if (val > maxOdd)
{
maxOdd = val;
}
}
else
{
if (val < minEven)
{
minEven = val;
}
}
}
}
/// missed the minimum of even part, thought max
int minEven = 2^40;
int i = 1;
while(i != 0)
{
string line = Console.ReadLine();
int.TryParse(line, out val);
i = val;
if (val != 0)
{
if (checkOdd(val))
{
if (val > maxOdd)
{
maxOdd = val;
}
}
else
{
if (val < minEven)
{
minEven = val;
}
}
}
}
My simple way in C++ using vector and map
#include<iostream>
#include<conio.h>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{
vector<int> array;
map<int,char> Earray,Oarray;
int n;
cout<<"Enter the numbers.... Enter zero to stop";
do{
cin>>n;
if(n!=0)
array.push_back(n);
}while(n!=0);
for(vector<int>::iterator i=array.begin();i!=array.end();i++)
{
if((*i)%2==0)
Earray[*i]='E';
else
Oarray[*i]='O';
}
map<int,char>::iterator it1=Earray.begin();
map<int,char>::iterator it2=Oarray.end();
it2--;
cout<<"Smallest even no is: "<<(*it1).first<<endl;
cout<<"Largest odd no is: "<<(*it2).first;
getch();
return 0;
}
1 #include<iostream>
2 #include<set>
3 using namespace std;
4
5 int main()
6 {
7 int n = -1;
8 set<int> oddmax;
9 set<int> evenmin;
10
11 while(cin >> n)
12 {
13 if(n == 0)
14 break;
15
16 if((n % 2) == 1)
17 oddmax.insert(n);
18 else
19 evenmin.insert(n);
20 }
21
22 set<int>::iterator it = oddmax.end();
23 --it;
24 set<int>::iterator it1 = evenmin.begin();
25
26 cout<<"Maximum of Odd #'s : "<<*it<<endl;
27 cout<<"Minimum of Even #'s : "<<*it1<<endl;
28
29 return 0;
30
31 }
1 #include<iostream>
2 #include<set>
3 using namespace std;
4
5 int main()
6 {
7 int n = -1;
8 set<int> oddmax;
9 set<int> evenmin;
10
11 while(cin >> n)
12 {
13 if(n == 0)
14 break;
15
16 if((n % 2) == 1)
17 oddmax.insert(n);
18 else
19 evenmin.insert(n);
20 }
21
22 set<int>::iterator it = oddmax.end();
23 --it;
24 set<int>::iterator it1 = evenmin.begin();
25
26 cout<<"Maximum of Odd #'s : "<<*it<<endl;
27 cout<<"Minimum of Even #'s : "<<*it1<<endl;
28
29 return 0;
30
31
}
1 #include<iostream>
2 #include<set>
3 using namespace std;
4
5 int main()
6 {
7 int n = -1;
8 set<int> oddmax;
9 set<int> evenmin;
10
11 while(cin >> n)
12 {
13 if(n == 0)
14 break;
15
16 if((n % 2) == 1)
17 oddmax.insert(n);
18 else
19 evenmin.insert(n);
20 }
21
22 set<int>::iterator it = oddmax.end();
23 --it;
24 set<int>::iterator it1 = evenmin.begin();
25
26 cout<<"Maximum of Odd #'s : "<<*it<<endl;
27 cout<<"Minimum of Even #'s : "<<*it1<<endl;
28
29 return 0;
30
31
}
1 #include<iostream>
2 #include<set>
3 using namespace std;
4
5 int main()
6 {
7 int n = -1;
8 set<int> oddmax;
9 set<int> evenmin;
10
11 while(cin >> n)
12 {
13 if(n == 0)
14 break;
15
16 if((n % 2) == 1)
17 oddmax.insert(n);
18 else
19 evenmin.insert(n);
20 }
21
22 set<int>::iterator it = oddmax.end();
23 --it;
24 set<int>::iterator it1 = evenmin.begin();
25
26 cout<<"Maximum of Odd #'s : "<<*it<<endl;
27 cout<<"Minimum of Even #'s : "<<*it1<<endl;
28
29 return 0;
30
31
}
can you just tell me why would one want to do a "--it"...i see all the soultions doing the same..if "it" is pointing to oddmax.end()...den that is the max rite? y shud you do --it...
also y "--it" instead of "it--"...i noe they r the same but any logical reason for following it?
// My Solution in C++ without any containers :)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout << "Enter the numbers:" << endl;
int maxOdd = 0;
int minEven = 0;
cout << "The numbers are: ";
int n;
cin >> n;
cout << n << " ";
if(n % 2 == 0){
minEven = n;
maxOdd = 0;}
else{
maxOdd = n;
minEven = 2 ^ 32; // The largest int
}
do {
char temp = cin.peek();
if (temp == '0')
break;
else if (temp == ' ')
temp = cin.get();
else{
cin >> n;
if ((n % 2 == 0) && (n < minEven))
minEven = n;
else if ((n % 2 != 0) && (n > maxOdd))
maxOdd = n;
cout << n << " ";
}
} while(true);
cout << "\nThe Maximum Odd number is: " << maxOdd << endl;
cout << "\nThe Minimum Even number is: " << minEven <<endl;
return 0;
}
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class MaxOddMinEven{
public static void main(String args[]){
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
try{
String inputStr = null;
int odd=0, even=0;
while(true){
inputStr = bReader.readLine();
System.out.println(inputStr);
try{
if(Integer.parseInt(inputStr)==0){
System.out.println("******************"+Integer.parseInt(inputStr));
break;
}else{
if((Integer.parseInt(inputStr))%2 !=0 ){
if(odd<Integer.parseInt(inputStr))
odd=Integer.parseInt(inputStr);
}
else{
if(even==0 || even>Integer.parseInt(inputStr))
even=Integer.parseInt(inputStr);
}
}
}catch(NumberFormatException ex){
ex.printStackTrace();
}
}
System.out.println("MaxOdd:" + odd + " MinEven:"+ even);
}catch(IOException ex){
ex.printStackTrace();
}
}
}
Using a vector and no sentinels. Using sentinel initializers somehow puts me off.
#include <iostream>
#include <vector>
using namespace std;
int main ( ) {
int val, max_odd, min_even;
vector<int> arr;
cout << "Enter the elements. Terminate with zero." << endl;
while (cin >> val && val != 0)
arr.push_back(val);
max_odd = min_even = 0;
/* Initialize max_odd and min even to the first odd
* and even numbers encountered. I don't prefer
* initializing with random sentinels, feels like bad
* coding to me
*/
for (int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 == 0)
min_even = arr[i];
else
max_odd = arr[i];
if (max_odd != 0 && min_even != 0)
break;
}
/* Loop through the entire vector and find
* the max_odd and min_even.
*/
for (int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 == 0 && arr[i] < min_even)
min_even = arr[i];
else if (arr[i] % 2 != 0 && arr[i] > max_odd)
max_odd = arr[i];
}
cout << "Max odd: " << max_odd << endl
<< "Min even: " << min_even << endl;
return 0;
}
#include <iostream>
int main()
{
using namespace std;
cout << "Enter a series of number (0 to stop): ";
int number ( 0 ), max_odd, min_even;
bool odd_flag( false ), even_flag ( false );
while ( cin >> number )
{
if ( 0 == number )
break;
if ( 0 != number % 2 ) // Odd number
{
if ( odd_flag )
{
if ( max_odd < number )
max_odd = number;
}
else
{
max_odd = number;
odd_flag = true;
}
}
else // Even number
{
if ( even_flag )
{
if ( min_even > number )
min_even = number;
}
else
{
min_even = number;
even_flag = true;
}
}
}
// Print output
if ( odd_flag )
cout << "Maximum odd number: " << max_odd << endl;
else
cout << "No odd number entered." << endl;
if ( even_flag )
cout << "Minimum even number: " << min_even << endl;
else
cout << "No even number entered." << endl;
return 0;
}
import java.util.ArrayList;
import java.util.Scanner;
public class maxodd {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);System.out.print("type in numbers and ends with 0");
ArrayList<Integer> arr=new ArrayList<Integer> ();
while(sc.hasNext()){
int in=sc.nextInt();
if(in==0) break;
arr.add(in);
}
int max=Integer.MIN_VALUE;
int min=Integer.MAX_VALUE;
for(int i=0;i<arr.size();i++){
if (arr.get(i)%2!=0){
if (arr.get(i)>max){
max=arr.get(i);
}
}else{
if (arr.get(i)<min){
min=arr.get(i);
}
}
}
System.out.println("the numbers are: "+arr.toString());
System.out.println(max);
System.out.println(min);
}
}
Python working code.
"""
6:00
@Python 2.7
Take a series of integers as input till a zero is entered. Among these given integers, find the maximum of the odd numbers and the minimum of the even integers (not including zero) and print them.
"""
class MaxOMinE(object):
def __init__(self, inputs):
if inputs is None:
print 'Invalid inputs'
raise SystemExit
self._inputs = inputs
self._maxOdd = None
self._minEven = None
def getResult(self):
for num in self._inputs:
# Terminate case
if num == 0:
print 'max odd: ', self._maxOdd
print 'min even: ', self._minEven
return
if num % 2 == 0: #even case
if self._minEven is None:
self._minEven = num
else:
if self._minEven > num:
self._minEven = num
else:
if self._maxOdd is None:
self._maxOdd = num
else:
if self._maxOdd < num:
self._maxOdd = num
# End of the inputs
print 'max odd: ', self._maxOdd
print 'min even: ', self._minEven
return
if __name__ == '__main__':
inputs = MaxOMinE([1,2,34,35,15])
inputs.getResult()
#include <stdio.h>
int main()
{
int in;
int maxodd = 0;
int mineven = 999;
while(1)
{
printf("enter input\n");
scanf("%d", &in);
if (in == 0)
break;
if (in%2 == 1)
{
if (in > maxodd)
maxodd = in;
}
else if (in%2 == 0)
{
if (in < mineven)
mineven = in;
}
}
printf("maxodd=%d mineven=%d", maxodd, mineven);
}
I have noticed that most of the submitted code takes an assumption that the minimum even value will be lesser the sum assumed value. Well this is a wrong assumption. Here is a working code without any assumption.
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int n, max=3, min, flag1=0, flag2=0;
while (1)
{
cin>>n;
if (n==0)
{
break;
}
if (flag1==0 && n%2==0)
{
min=n;
flag1=1;
}
else if (flag2==0 && n%2!=0)
{
max=n;
flag2=1;
}
else if (n%2==1 && flag2!=0)
{
if (n>max)
{
max=n;
}
}
else if(n%2==0 && flag1!=0)
{
if (n<=min)
{
min=n;
}
}
}
cout<<"max of odd numbers ="<<max<<endl;
cout<<"min of even number ="<<min<<endl;
return 0;
}
I have noticed that most of the submitted code takes an assumption that the minimum even value will be lesser the sum assumed value. Well this is a wrong assumption. Here is a working code without any assumption.
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int n, max=3, min, flag1=0, flag2=0;
while (1)
{
cin>>n;
if (n==0)
{
break;
}
if (flag1==0 && n%2==0)
{
min=n;
flag1=1;
}
else if (flag2==0 && n%2!=0)
{
max=n;
flag2=1;
}
else if (n%2==1 && flag2!=0)
{
if (n>max)
{
max=n;
}
}
else if(n%2==0 && flag1!=0)
{
if (n<=min)
{
min=n;
}
}
}
cout<<"max of odd numbers ="<<max<<endl;
cout<<"min of even number ="<<min<<endl;
return 0;
}
List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(2,5,6,7,8,0));
int maxOdd = numbers.get(0);
int minEven = numbers.get(0);
for(int i = 0; i < numbers.size() - 1; i++)
{
if(numbers.get(i) % 2 == 0 && numbers.get(i) < minEven)
{
minEven = numbers.get(i);
}
else if(numbers.get(i) % 2 != 0 && numbers.get(i) > maxOdd)
{
maxOdd = numbers.get(i);
}
}
System.out.println(minEven + " " + maxOdd);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int in=-1;
vector<int> v1,v2;
while ( in!=0 )
{
cin>>in;
if ( in!=0 &&in%2==0 )
{
v1.push_back ( in );
}
if ( in!=0 &&in%2==1 )
{
v2.push_back ( in );
}
}
cout<<*max_element ( v2.begin(),v2.end() ) <<" "<<*min_element ( v1.begin(),v1.end() );
getchar();
}
Another solution without any containers and catering negative integers
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int in=-1;
int min=INT_MAX,max=INT_MIN;
while ( in!=0 )
{
cin>>in;
if ( in!=0 &&in%2==0 )
{
if ( min>in ) {
min=in;
}
}
if ( in!=0 && ( in%2==1 || in%2==-1 ) )
{
if ( in>max ) {
max=in;
}
}
}
cout<<max <<" "<<min;
getchar();
}
- roy March 10, 2012