GE (General Electric) Interview Question






Comment hidden because of low score. Click to expand.
1
of 1 vote

sum may overflow. I'd recommend to recalculate the average at each iteration:
AVG = AVG * (n-1) / n + x / n

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

May i Know what is this x here ?? and the logic for this ??

- Ashok September 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

When you say 'return', do you mean display? Or did they expect you to return multiple values in an array or date structure?

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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())};
}

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how about passing three references??

- nikhil May 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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");
        }        
    }

- pjane June 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am not returning values here, just simply displaying them.. If you want to return, create an object of 3 ints or simply return a float array..

- pjane June 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

return doesn't mean return value necessarily. You can have a function having following prototype:
void linkList_min_max_avg(LinkList * rootNode,
double& outMin,
double& outMax,
double& outAvg );

This way you can still get the values back from the function.

- Abhinav July 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//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;

}

- shashank Kumar July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//
// 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;
}

- CodeWarrior September 12, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More