UTMB Interview Question for System Administrators


Country: United States




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

Base on your code (Edited as suggestion of Fred):

public static void main (String[] args) {
        // When initial value for min, max, SecondMax, SecondMin
        // I think it's better if we using 0 (break number)
        // than using Integer.MAX_VALUE and Integer.MIN_VALUE
        // because there value my be using for input and
        // we never meet 0 so it's more easy for us checking is there
        // exist the answer or not in the end.
        int min = 0;
        int max = 0;
        int SecondMax = 0;
        int SecondMin = 0;

        Scanner s = new Scanner(System.in);
        while (true) {
            System.out.print("Enter a Value: ");
            int val = s.nextInt();

            if (val == 0) {
                break;
            }

            // when a value less than or equal min val,
            // this value will be new min and current min value
            // may be second min value.
            // Be careful with equals value as: 2 2 3 3 3
            // so we using equal there.
            if (min == 0 || val <= min) {
                if (SecondMin == 0 || SecondMin > min) {
                    SecondMin = min;
                }
                min = val;
            } else if (SecondMin == 0 || (val >= min && val < SecondMin)) {
                SecondMin = val;
            }

            // as min value
            if (max == 0 || val >= max) {
                if (SecondMax == 0 || SecondMax > max) {
                    SecondMax = max;
                }
                max = val;
            } else if (SecondMax == 0 || (val <= max && val > SecondMax)) {
                SecondMax = val;
            }
        }

        // be careful with edge case, there is less than 2 element
        // in this case we don't have second smallest and largest
        // and may be we don't have smallest and largest.
        if (min != 0) {
            System.out.println("The smallest number is: " + min);
        } else {
            System.out.println("We don't have smallest number.");
        }

        if (max != 0) {
            System.out.println("The largest number is: " + max);
        } else {
            System.out.println("We don't have largest number.");
        }

        if (SecondMin != 0) {
            System.out.println("The second smallest number is: " + SecondMin);
        } else {
            System.out.println("We don't have second smallest number.");
        }

        if (SecondMax != 0) {
            System.out.println("The second largest number is: " + SecondMax);
        } else {
            System.out.println("We don't have second largest number.");
        }
    }

- techinterviewquestion.com February 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Im not sure this works because you dont check to compare val to the secondMax or secondMin. For example if you had a number smaller then secondMin but larger than min it doesnt change secondMin.

- Fred February 02, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you!

- BrowerLot February 02, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Fred, my solution doesn't correct, I will correct this soon.

- techinterviewquestion.com February 02, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another approach

Console.Clear();
                node n = new node { Parent = null };
                int counter = 0;
                int smallest = -1, largest = 0, secSmallest = 0, secLargest = 0;
                while (true)
                {
                    int ip = Convert.ToInt32(Console.ReadLine());
                    if (ip == 0)
                        break;
                    counter++;
                    if (smallest > ip || smallest == -1)
                        smallest = ip;
                    if (largest < ip)
                        largest = ip;
                    node temp = new node { value = ip };
                    temp.Parent = n;
                    n = temp;
                }
                if (counter > 2)
                {
                    secSmallest = largest;
                    secLargest = smallest;
                    while (n.Parent != null)
                    {
                        if (n.value > smallest && n.value <= secSmallest)
                            secSmallest = n.value;
                        if (n.value < largest && n.value >= secLargest)
                            secLargest = n.value;
                        n = n.Parent;
                    }
                    Console.WriteLine("Smallest : {0}  Sec Smallest : {1}  Sec Largest : {2}  Largest : {3}  ", smallest, secSmallest, secLargest, largest);
                }
                else if (counter ==0 || counter == 1)
                {
                    Console.WriteLine("Not enough data to calculate");
                }else{
                    Console.WriteLine("Smallest : {0}  Sec Smallest : {1}  Sec Largest : {2}  Largest : {3}  ", smallest, "No Data", "No Data", largest);
                }
                
                Console.ReadLine();

- A3Clef February 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import sys

def smallestLargerNoArrays(stopToken):
    print('Enter one number per line\n')
    maximun, secondMaximun, minimun, secondMinimun = None,None,None,None
    
    while (True):
        userInput = input()
        if (userInput == stopToken):
            print ('Maximun:{0} Second Maximun:{1} Minimun:{2} Second Minimun:{3}').format(maximun,
                                                                                               secondMaximun if secondMaximun != None else maximun,
                                                                                               minimun,
                                                                                               secondMinimun if secondMinimun != None else minimun)
            return
        secondMaximun = max(secondMaximun,min(userInput,maximun))

        maximun = max(userInput,maximun)

        if (secondMinimun == None and minimun != None): secondMinimun = max(userInput,minimun)
        elif(secondMinimun != None): secondMinimun = min(secondMinimun, max(userInput,minimun))

        minimun = userInput if minimun == None else min(userInput,minimun)

- sergio February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h> 
int main() {
	int max = 0, min = 0, scndMax = 0, scndMin = 0, val = 0; 
	while(scanf("%d", &val) && val) {
		if(val >= max) {
			scndMax = max; 
			max = val; 
		}else {
			if(scndMax < val) scndMax = val; 
		}
		if(val <= min) {
			scndMin = min; 
			min = val; 
		}else {
			if(scndMin > val) scndMin = val; 
		}
	}
	printf("Max value %d\n", max); 
	printf("Second max value %d\n", scndMax); 
	printf("Min value %d\n", min); 
	printf("Second min value %d\n", scndMin); 
}

- M.Sayed February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main()
{
    int max=-999,s_max=-9997,n;
    int min=9999,s_min=9997;
    while(true)
    {
        cin>>n;
        if(n==0)
            break;
        if(n>max&&n>s_max)
        {s_max=max;max=n;}
        else if(n<max&&n>s_max)
            s_max=n;
        if(min>n&&s_min)
        {
            s_min=min;min=n;
        }
        else if(min<n&&s_min>n)
        {
            s_min=n;
        }


    }
    cout<<min<<" "<<s_min;
}

- sahiljulka44 February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <limits.h>

using namespace std;

int main(int argc, char** argv)
{
int value = 0;
int min = INT_MAX;
int max = 0;
int secondmin = INT_MAX;
int secondmax = 0;

do
{
cin >> value;
if(value == 0 ) break;
if(value < min )
{
secondmin = min;
min = value;
}
else if (value < secondmin)
{
secondmin = value;
}
else if (value > max)
{
secondmax = max;
max = value;
}
else if (value > secondmax)
{
secondmax = value;
}
}
while(cin);

cout << "Min " << min << " secondmin " << secondmin << " secondmax " << secondmax << " max " << max << endl;


return 0;
}

- Rishipal February 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <limits.h>

using namespace std;

int main(int argc, char** argv)
{
	int value = 0;
	int min = INT_MAX; 
	int max = 0;
	int secondmin = INT_MAX;
	int secondmax = 0;

	do
	{
		cin >> value;
		if(value == 0 ) break;
		if(value < min )
		{
			secondmin = min;
			min = value;
		}
		else if (value < secondmin)
		{
			secondmin = value;
		}
		else if (value > max)
		{
			secondmax = max;
			max = value;
		}
		else if (value > secondmax)
		{
			secondmax = value;
		}
	}
	while(cin);

	cout << "Min " << min << " secondmin " << secondmin << " secondmax " << secondmax << " max " << max << endl;


	return 0;
}

- Rishipal February 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		int secMax = Integer.MIN_VALUE;
		int secMin = Integer.MAX_VALUE;
		
		Scanner s = new Scanner(System.in);
		max = min = secMax = secMin = s.nextInt();
		while(true){
			System.out.println("Enter a value: ");
			int value = s.nextInt();
			
			if(value == 0){
				break;
			}
			
			if(value > secMax){
				if(value > max){
					secMax = max;
					max = value;
				}else{
					secMax = value;
				}
			}
			
			if(value < secMin){
				if(value < min){
					secMin = min;
					min = value;
				}else{
					secMin = value;
				}
			}
		}
	
		System.out.println("Max: " + max);
		System.out.println("Min: " + min);
		System.out.println("Sec Max " + secMax);
		System.out.println("Sec Min " + secMin);
	}

- Anonymous February 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		int secMax = Integer.MIN_VALUE;
		int secMin = Integer.MAX_VALUE;
		
		Scanner s = new Scanner(System.in);
		max = min = secMax = secMin = s.nextInt();
		while(true){
			System.out.println("Enter a value: ");
			int value = s.nextInt();
			
			if(value == 0){
				break;
			}
			
			if(value > secMax){
				if(value > max){
					secMax = max;
					max = value;
				}else{
					secMax = value;
				}
			}
			
			if(value < secMin){
				if(value < min){
					secMin = min;
					min = value;
				}else{
					secMin = value;
				}
			}
		}
	
		System.out.println("Max: " + max);
		System.out.println("Min: " + min);
		System.out.println("Sec Max " + secMax);
		System.out.println("Sec Min " + secMin);
	}

- LateRunner February 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void sorting() throws NumberFormatException, IOException{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
int max = 0;
int min = Integer.parseInt(br.readLine());
while(true){


int i = Integer.parseInt(br.readLine());
if(i == 0){
break;
}
if(max < i){
max = i;
}
if(min > i){
min = i;

}

}
System.out.println("max = " + max);
System.out.println("min = " + min);

}

- Hitesh September 23, 2016 | 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