Epic Systems Interview Question
System Administrators/*
Since the question said "gives a set of numbers ", so we should consider all situations, negative, positive, float or integer. And I used linked list here because we don't know how many numbers the user will input, so the linked list is the best way.
*/
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node{
float data;
struct Node * pNext;
}NODE, * PNODE;
PNODE Init(int * min, int * max);
void Traverse(PNODE pHead);
int main(void){
int minEven = 100000;
int maxOdd = -999999;
PNODE pHead = Init(&minEven, &maxOdd);
Traverse(pHead);
printf("minEven = %d, maxOdd = %d\n", minEven, maxOdd);
return 0;
}
PNODE Init(int * min, int * max){
PNODE pHead = (PNODE)malloc(sizeof(NODE));
PNODE pTail = pHead;
pTail -> pNext = NULL;
float value = -999;
if ( pHead == NULL )
exit(-1);
else{
while(value != 0){
PNODE pNew = (PNODE)malloc(sizeof(NODE));
scanf_s("%f", &pNew -> data);
value = pNew -> data;// value is the data that you should operate
if( value == (int) value && value != 0){ // we only compare the integer value
int intVal = (int)value;
if(abs(intVal % 2) == 1){
if ( intVal >= *max)
*max = intVal;
}
else if(intVal %2 == 0){
if (intVal <= *min)
*min = intVal;
}
}
pTail -> pNext = pNew;
pTail = pNew;
pTail -> pNext = NULL;
}
}
return pHead;
}
void Traverse(PNODE pHead){
PNODE pCur = pHead -> pNext;
while(pCur != NULL){
printf("%.2f ", pCur -> data);
pCur = pCur -> pNext;
}
printf("\n");
}
import java.util.Scanner;
public class MaxOddMinEven {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long minEven = Long.MAX_VALUE;
long maxOdd = -1;
while (true) {
long nextLong = input.nextLong();
if (nextLong == 0) {
input.close();
System.out.println("Max Odd Number: " + maxOdd);
System.out.println("Min Even Number: " + minEven);
break;
}
if (nextLong % 2 == 0) {
if (nextLong < minEven) {
minEven = nextLong;
}
} else {
if (nextLong > maxOdd) {
maxOdd = nextLong;
}
}
}
}
}
Output:
1
2
3
4
5
6
7
8
0
Max Odd Number: 7
Min Even Number: 2
As we read on inputs, store the maximum odd int and minimum even int so far in two respective variables. At the end of all the inputs (user enters 0), we have ready the required output.
- cuppanomics May 11, 2010The company stressed on considering all input values i suppose, more of fuzzing logic (boundary conditions for inputs and stuff).