GE (General Electric) Interview Question
for actual 'return' value:
/**
* Takes in a list and returns the min, max, and average in an array.
* @param list The list to process, must have at least 1 element, and not be null.
* @throws IllegalArgumentException if the list is null or empty.
* @return A Number array. Index 0 = min, index 1 = max, and index 2 = average.
*/
public Number[] processList(final List<Number> list) {
List<Number> temp_list = new LinkedList<Number>(list.size());
if (list == null || (list != null && list.size() == 0)) {
throw new IllegalArgumentException("Invalid list provided!");
}
Number min = list.get(0);
Number max = list.get(0);
Number sum = 0;
while (!list.isEmpty()) {
Number n = list.remove(0);
temp_list.add(n);
min = n < min ? n : min;
max = n > max ? n : max;
sum += n;
}
list = temp_list;
return new Number[] {min, max, (sum / list.size())};
}
public static void traverse(LinkedList l){
try{
int counter = 0;
int min = (Integer) l.get(counter);
int max = (Integer) l.get(counter);
int avg = (Integer) l.get(counter);
counter++;
while(counter<l.size()){
int element = (Integer) l.get(counter);
if(element < min){
min = element;
}
if(element > max){
max = element;
}
avg = avg + element;
counter++;
}
System.out.println("Avg:" + avg/l.size() + "Min:" + min + "Max:" + max);
}
catch(NullPointerException e){
System.out.println("NoList");
}
catch(IndexOutOfBoundsException i){
System.out.println("Outofbounds");
}
}
//Language C++
struct Node
{
int data;
struct Node* next;
};
typedef struct Node* NodePtr;
void GetMinMaxAvg(NodePtr head,int& min,int& max,int& avg)
{
min = MAX_INT;
max = MIN_INT;
int sum = 0,count=0,data=0;
NodePtr current = head;
while(current)
{
data = current->data;
if(min > data)
min = data;
if(max < data)
max = data;
sum += data;
++count;
current = current->next;
}
avg = sum/count;
}
//
// main.m
// CSAlgorithms
//
// Created by Samuel Sharaf on 9/9/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
// define the function templates here we are going to use
// define the node struct
struct node {
int data;
struct node* next;
};
// 1. create a ll with headnode
struct node* buildLL();
// 2. define the push function which will add nodes to the beginning of the list
struct node* pushNode(struct node** headRef, int data);
struct node* pushNodeToTail(struct node** headRef, int data);
// 3. define the length function which will return the length of the ll given the head ptr
int getLength(struct node* headPtr);
void printLL(struct node* headPtr);
int getMaxVal(struct node*);
int getMinVal(struct node*);
int averageOfLLEndPoints(struct node* head);
int main(int argc, const char * argv[])
{
@autoreleasepool {
struct node* headNode = buildLL();
for (int i = 1; i <=7 ; i++) {
pushNode(&headNode, i);
}
printLL(headNode);
printf("\nThe max value in the LL is : %d", getMaxVal(headNode));
printf("\nThe min value in the LL is : %d", getMinVal(headNode));
printf("\nThe avg value in the LL is : %d", averageOfLLEndPoints(headNode));
}
return 0;
}
int getLength(struct node* headNode)
{
struct node* current = headNode;
int length = 0;
while (current->next != NULL) {
current = current -> next;
length++;
}
//printf("\n-----Returning length of the LL as :%d", length);
return length;
}
int getMaxVal(struct node* headNode)
{
struct node* current = headNode;
int maxVal = current -> data;
printf("\nThe current maxVal is: %d", maxVal);
int length = getLength(current);
for (int i = 1; i < length; i++) {
current = current -> next;
int curVal = current -> data;
printf("\nthe curVal is: %d", curVal);
if(curVal > maxVal) {
maxVal = curVal;
}
}
return maxVal;
}
int getMinVal(struct node* headNode)
{
struct node* current = headNode;
int minVal = current -> data;
int length = getLength(current);
for (int i = 1; i < length; i++) {
current = current -> next;
int curVal = current -> data;
printf("\nthe curVal is: %d", curVal);
if(curVal < minVal) {
minVal = curVal;
}
}
return minVal;
}
int averageOfLLEndPoints(struct node* head)
{
int firstVal = head->data;
while (head->next != NULL) {
head = head -> next;
}
int lastVal = head->data;
printf("The firstVal is %d and lastVal is %d", firstVal, lastVal);
return (firstVal+lastVal)/2;
}
void printLL(struct node* headNode)
{
int count = 0;
struct node* current = headNode;
while (current -> next != NULL) {
current = current -> next;
printf("\nthe data in the node is: %d", current-> data);
count++;
}
printf("The length of the LL is: %d", count);
}
struct node* pushNode(struct node** headRef, int data)
{
//create a new node
struct node* newNode = malloc(sizeof(struct node));
newNode -> next = *headRef;
newNode -> data = data;
*headRef = newNode;
return newNode;
}
struct node* buildLL()
{
struct node* headNode = malloc(sizeof(struct node));
headNode -> data = 0;
headNode -> next = NULL;
return headNode;
}
sum may overflow. I'd recommend to recalculate the average at each iteration:
- Anonymous June 14, 2011AVG = AVG * (n-1) / n + x / n