Persistent Systems Interview Question


Country: India




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

1.Add a 0 to the array.
2. Sort the array ,
3. Find the location of 0 in the sorted array
4. Find the elements closes to 0..can be before or after
5. Return their sum
There is a edge case that a 0 already exists in which case you would slightly modify the logic to find the position of nearest values. I have not coded that. Time efficiency O(nLgn) for the sort and O(n) for iterating over the array. space complexity O(n).

public static int getSumOfClosestToZeroElements(int[] input) {
		int sum = 0;
		
		if (input == null) {
			return 0;
		} else if (input.length == 1) {
			return input[0];
		} else if (input.length == 2) {
			return input[0] + input[1];

		}
		
		int[] tempArray = new int[input.length + 1];
		
		for (int index = 0; index < input.length; index++) {
			tempArray[index] = input[index];
		}
		tempArray[tempArray.length - 1] = 0;
		Arrays.sort(tempArray);
		int zeroIndex = getZeroIndex(tempArray);
		if (zeroIndex == 0) {
			sum = tempArray[1] + tempArray[2];
		} else if (zeroIndex == tempArray.length - 1) {
			sum = tempArray[zeroIndex - 1] + tempArray[zeroIndex - 2];
		} else {
			sum = tempArray[zeroIndex - 1] + tempArray[zeroIndex + 1];
		}
		return sum;
	}

	public static int getZeroIndex(int[] array) {
		int index = 0;
		
		for (; index < array.length; index++) {
			if (array[index] == 0) {
				break;
			}
		}
		return index;
	}

- APill March 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

The simplest way of solving this problem:
step1: Sort the numbers strictly by considering absolute values.(java program)
step2: Add first two elements of the sorted array
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();


}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];

System.out.print(" "+answer);

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

The simplest way of solving this problem:
step 1: sort the array by considering absolute values.
step 2: Add first two elements of the sorted array.
code-
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();


}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];

System.out.print(" "+answer);
}

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

The simplest way of solving this problem:
step 1: sort the array by considering absolute values.
step 2: Add first two elements of the sorted array.
code-
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();


}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];

System.out.print(" "+answer);
}

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

I think this can be resolved by scan the array and keep the two numbers closest to 0. It will only need O(n). The python function:

def get_sum_of_2_numbers_close_to_0(a):
    if type(a) is not list or len(a) < 2:
        raise Exception("The input must be an array with at least 2 numbers")
    import sys

    min_num = [sys.maxsize]*2
    for k in a:
        if abs(k) < abs(min_num[1]):
            min_num[1] = k
            if abs(min_num[1]) < abs(min_num[0]):
                min_num[0], min_num[1] = min_num[1], min_num[0]
    return min_num[0]+min_num[1]

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

def sum_closest_to_zero_revised(a):
    if len(a) <= 2:
        return sum(a)
    else:
        closestOneDiff, closesTwoDiff = abs(a[0]-0), abs(a[1]-0)
        if closestOneDiff < closesTwoDiff:
            result = {"Smallest": a[0], "SecondSmallest": a[1]}
        else:
            result = {"Smallest": a[1], "SecondSmallest": a[0]}
        for n in xrange(2,len(a)):
            if abs(a[n] - 0) > abs(result["SecondSmallest"]-0):
                pass
            elif abs(a[n] - 0) <= abs(result["SecondSmallest"]-0) and abs(a[n] - 0) > abs(result["Smallest"]-0):
                result["SecondSmallest"] = a[n]
            else:
                result["SecondSmallest"] = result["Smallest"]
                result["Smallest"] = a[n]
        return result

- PyDing March 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// ZoomBA :: Trivial in it 
h = heap(2) ::{ #|$.o.0| < #|$.o.1| }
h += [ -5, 13, 5 ]
println ( sum(h) )

- NoOne October 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The simplest method to solve this problem:
Step 1: sort the array by taking absolute values.
Step 2: Add first two elements of the sorted array.
code-
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] numbers=new int[n];
for(int i=0;i<n;i++)
{
numbers[i]=in.nextInt();


}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (Math.abs(numbers[i]) > Math.abs(numbers[j]))
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int answer=numbers[0]+numbers[1];

System.out.print(" "+answer);
}

- Megha March 02, 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