Zycus Interview Question for Software Engineer / Developers






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

Try this one....

import java.io.*;

class Mindist
{
	public static void main(String args[]) throws java.io.IOException
	{
		System.out.println("Enter the number of elements in the array: ");
		int k=Integer.parseInt(System.console().readLine());
		int a[]=new int[k];
		int min,i;
		int f=0;
		int p=0;
		System.out.println("Enter the array of numbers: ");
		for(i=0;i<a.length;i++)
		{
			a[i]=Integer.parseInt(System.console().readLine());
		}
		min=a[0];
		for(i=0;i<a.length;i++)
		{
			if(Math.abs(a[i])<Math.abs(min))
			{
				min=a[i];
				p=i;
			}
			else
			continue;
		}
		for(i=0;i<a.length;i++)
		{
			if(i!=p && a[i]==Math.abs(min))
			{
				f=1;
			}
			else
			continue;
		}
		System.out.println("Minimum distance value is ");
		if(f==1)
		{
			System.out.println(+Math.abs(min));
		}
		else
		{
			System.out.println(+min);
		}
	}
}

- Ajay Narvekar August 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yupp... this works correctly....

- Debu(r)ggerrrr August 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

null ptr exception

- anil August 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

try this one:

import java.io.*;

class Mindist
{
	public static void main(String args[]) throws java.io.IOException
	{
		int[] a=new int[5];
		int min,i;
		System.out.println("Enter the array of numbers: ");
		for(i=0;i<a.length;i++)
		{
			a[i]=Integer.parseInt(System.console().readLine());
		}
		min=Math.abs(a[0]);
		for(i=0;i<a.length;i++)
		{
			if(Math.abs(a[i])<min)
			{
				min=Math.abs(a[i]);
			}
			else
			continue;
		}
		System.out.println("Minimum distance value is "+min);
	}
}

- Ajay Narvekar July 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This code has a bug.
For input -1,2,3,4,5 it will give output as 1,whereas this number is not present in the initial array

- DaBugger July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

OK... Thanks DaBugger....
Working on it....
See the solution in next reply to the question...

- Ajay Narvekar August 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For every number A[i] (except A[i] == 0), takes absolute value of A[i]. Then compute the smallest number. To resolve tie, another scanning would do the job.

- lol May 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
You dont need second scanning In the first scan itself if difference is a tie and number is positive then replace it with existing one if its negative dont replace {{{ int retval = a[0],mindist = abs(a[0]); for(i=1;i<n;i++){ if(abs(a[i]) > mindist) continue; if(abs(a[i]) == mindist && a[i] >0) retval = a[i]; else{ mindist = abs(a[0]); retval = abs(a[i]) } } - Abhi May 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You dont need second scanning In the first scan itself if difference is a tie and number is positive then replace it with existing one if its negative dont replace

int retval = a[0],mindist = abs(a[0]); 
 for(i=1;i<n;i++){
     if(abs(a[i]) > mindist) continue;
     if(abs(a[i]) == mindist && a[i] >0) retval = a[i];
     else{
        mindist = abs(a[0]);
        retval = abs(a[i])
     }
 }

- Abhi May 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good idea. But i think there is a mistake in your code

retval = abs(a[i])

should be

retval = a[i]

- lol May 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is working perfectly guys....

import java.util.Arrays;


public class FindNeartoZero {
	
	public static void main(String[] args) {
		int a[]={-10,-20,-30,-10,-50,-1,1};
		Arrays.sort(a);
		int i;
		for(i=0;i<a.length;i++)
		{
			if(a[i]>0)
			{
				if(i==0)
				{
					System.out.println(a[i]);
				     System.exit(0);
				}
				int k=a[i-1];
				System.out.println(Math.abs(k)<Math.abs(a[i])?k:a[i]);
				System.exit(0);
			}
		}
		if(i==a.length)
			System.out.println(a[--i]);
	}
}

- kumarasvn May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This might work...(simple logic)

#include<iostream>
#include<stdio.h>
int
 main() {
 int a[]={-10,-20,-30,-10,-50,-1,1};
 int n=7, min=9999999;

 for(int i=0; i< n; i++) {

   if(a[i] > 0)
   {
    if( a[i] < min)
     min=a[i];
   }

  else
    if( (0-a[i]) < min)
     min = 0-a[i];
 }

  printf(" %d\n", min);
  return 0;
 }

- raja roy June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey10873" class="run-this">set data "-1 6 4 -5 9 -3 -2"
set min [ expr abs([ lindex $data 0 ]) ]
set result [ lindex $data 0 ]
for { set i 1 } { $i < [ llength $data ] } { incr i } {
if { [ expr abs([ lindex $data $i ]) ] < $min } {
set min [ expr abs([ lindex $data $i ]) ]
set result [ lindex $data $i ]
} elseif { [ expr abs([ lindex $data $i ]) ] == $min } {
set result [ expr abs([ lindex $data $i ])]
}
}
puts $result</pre><pre title="CodeMonkey10873" input="yes">
</pre>

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

Hi All, This code logic is working fine . In all conditions....

// number near to zero
	
	$array = array(-87,-90,-67,45,67,-340,-45);
	
	$array = array(-1,2,3,4,-2);
   	
	// initialize nearToZero variable with $array[0] absolute value
	
	$nearToZero = abs($array[0]);
	$pos = 0;
	
	for($i=1;$i<count($array);$i++)
	{
	 	//check $array[$i] is bigger than $nearToZero or not
		if(abs($array[$i])<abs($nearToZero))
		{
			$nearToZero = abs($array[$i]);
			$pos = $i;
		}
		
		
		else if(abs($array[$i]) == abs($nearToZero))
		{
			if($array[$i]>0)
			{
				$pos = $i;	
			}
			
		} 
		
		/*if($nearToZero<0)
		{
			if()
		} */	
	}
	
	
		echo ' near to zero :- '.$nearToZero.' position of number :- '.$pos;

- Yogendra Gautam January 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int closest=Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++)
{
if(Math.abs(arr[i])>=0 && Math.abs(arr[i]<closest)
{
closest=arr[i];
}
}
System.out.println("The closest is"+closest);

- aa1992 September 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int closest=Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++)
{
if(Math.abs(arr[i])>=0 && Math.abs(arr[i])<closest)
{
closest=arr[i];
}
}
System.out.println("The closest is"+closest);

- aa1992 September 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class NearestToZero {

public static void main(String[] args) {

int positiveNearest = Integer.MAX_VALUE;
int negativeNearest = ((-1) * Integer.MIN_VALUE);
int nearest = 0;


int[] a = { -5,6,2,-1,-2,1 };

for(int i = 0 ; i < a.length ; i++) {
if(a[i] > 0 && a[i] < positiveNearest) { // for positive integers
positiveNearest = a[i];
nearest = positiveNearest;
}else if(a[i] < 0 && a[i] > negativeNearest) { // for negative integers
negativeNearest = a[i];
nearest = negativeNearest;
}
}

if((positiveNearest + negativeNearest) == 0)
nearest = positiveNearest;

System.out.println(positiveNearest + " : " + negativeNearest);

System.out.println("Nearest to zero : " + nearest);

}

}

- Gaurav Singh May 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class NearestToZero {

	public static void main(String[] args) {
	
		int positiveNearest = Integer.MAX_VALUE;
		int negativeNearest  = ((-1) * Integer.MIN_VALUE);
		int nearest = 0;
		
		
		int[] a = { -5,6,2,-1,-2,1 };
		
		for(int i = 0 ; i < a.length ; i++) {
			if(a[i] > 0 && a[i] < positiveNearest) { 	// for positive integers
				positiveNearest = a[i];
				nearest = positiveNearest;
			}else if(a[i] < 0 && a[i] > negativeNearest) {						// for negative integers
				negativeNearest = a[i];
				nearest = negativeNearest;
			}
		}
		
		if((positiveNearest + negativeNearest) == 0)
			nearest = positiveNearest;
		
		System.out.println(positiveNearest + " : " + negativeNearest);
		
		System.out.println("Nearest to zero : " + nearest);

	}

}

- Gaurav Singh May 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class CodePractice {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*1.An array contains a set of +ve and –ve numbers.
Find the number that is nearest to zero. If two numbers are equally near (like 2 and -2) then return +ve number.*/
int a[] = {-3, 8, 2, 7, -2, -9, 1};
int min = Integer.MAX_VALUE;
for(int i=0; i< a.length; i++){
if(a[i] < min && a[i]> 0){
min = a[i];
}
}
System.out.println(min);
}
}

- pravin September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello,
This code has bug...
This code will give wrong output for input array, say (-1,2,3,4,-2)
output for this input should be -1 but your code will give 2 as output.
Try again. Have a look on the code which I've posted having 3 likes on the top of this page.

- Ajay Narvekar September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

also this will give wrong output for the input like (-1, 2,3,4,5).
here correct answer is -1 but your code will give 2 as o/p

- Ajay Narvekar September 11, 2013 | Flag


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