UTMB Interview Question
System AdministratorsCountry: United States
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.
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();
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)
#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);
}
#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;
}
#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;
}
#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;
}
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);
}
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);
}
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);
}
Base on your code (Edited as suggestion of Fred):
- techinterviewquestion.com February 02, 2016