Oracle Interview Question for Backend Developers


Country: India




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

Mayn't be well optimized but should work.

/**
	 * Replace each number with the next bigger number from right side of current index.
	 * If no bigger number found, print the no itself.
	 * E.g.
	 * input;  2,5,9,6,3,4,8,15,12
	 * output: 3,6,12,8,4,8,12,15,12
	 */
	public static void nextBiggerNumber() {
		int in[] = {2,5,9,6,3,4,8,15,12};
		int out[] = new int[in.length];
		int nextBigNo;
		boolean found = false;
		
		
		//Find the next big no for all n-1 elements
		for (int j=0; j<in.length-1; j++) {
			
			found = false;
			nextBigNo = in[j];
			
			for (int k=j+1; k<in.length; k++) {
				if (in[k] > in[j]) {
					if (!found) {
						nextBigNo = in[k];
						found = true;
					}else {
						if (in[k] < nextBigNo) 
							nextBigNo=in[k];
					}				
					
				}
			}
			
			out[j]=nextBigNo;
			
		}
		
		//Copy the last element
		out[in.length -1] = in[in.length-1]; 
		
		for (int i=0; i<out.length; i++) {
			System.out.print(out[i] + ", ");
		}
	}

- SC December 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would work. But it takes O(n*n), looking for an optimized Solution.

- SRC December 19, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since the order of given numbers is not guaranteed, i don't think we have another option to achieve this less than O(n*n)

- Jogi December 21, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it not possible to solve the problem in less than O(n*n)?

- Jagadeesh December 25, 2018 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

nLogn solution is possible.

Create a map ( a balanced BST for log n lookup ).
insert last item in the map.
[1] Start from the second last element in the list and go backwards .
[2] for every element find next bigger element in map ( something like upper_bound in C++ ). And use that result in the result array. insert current element in original array into map ( so its available for next lookup).

- Coder December 26, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Replace each number with the next bigger number from right side of current index.
# program is in python version 3...
arr_input = [2,5,9,6,3,4,8,15,12];#input array......
arr_result = arr_input;#result or output of our input....
print(arr_input)
for i in range(len(arr_input)):
    temp = arr_input[i]
    remainingelements = sorted(arr_input[i:])#make a sorted array of remaining elements on right side
    for j  in range(len(remainingelements)):
         if(remainingelements[j]>temp):#check for greatest element on the right side....
             arr_result[i] = remainingelements[j]#insert to the output
             break

# print the output
print(arr_result)

- hamzajamshaid95 December 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Replace each number with the next bigger number from right side of current index.
# program is in python version 3...
arr_input = [2,5,9,6,3,4,8,15,12];#input array......
arr_result = arr_input;#result or output of our input....
print(arr_input)

for i in range(len(arr_input)):
    temp = arr_input[i]
    remainingelements = sorted(arr_input[i:])#make a sorted array of remaining elements on right side

    for j  in range(len(remainingelements)):

         if(remainingelements[j]>temp):#check for greatest element on the right side....
             arr_result[i] = remainingelements[j]#insert to the output
             break

# print the output
print(arr_result)

- hamzajamshaid95 December 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

~ replace each number in a list by its next bigger number.
~ No replacing if the number is the biggest or at the end of the list.
~ code in Phyton 3

def fun (lst):
    work = lst
    for i in range(0,len(work)):
        if work[i] == max(work) or i == len(work)-1:
            continue
        else:
            temp = [x for x in work if x > work[i]]
            work[i] = min(temp)
    return work

- bobysamuel123 December 21, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;
class replaceNumbers{ 
	public static void main(String[] args) {
		int in[] = {2,5,9,6,3,4,8,15,12};		
		System.out.println(Arrays.toString(nextBigNumbers(in)));
	}

	public static int[] nextBigNumbers(int[] in) {
		int out[] = new int[in.length];	
		int k=0;
		int num=0;
		boolean[] check = new boolean[in.length];
		for(int i=0;i<in.length;i++){
			int diff=Integer.MIN_VALUE;
			int mindiff=Integer.MAX_VALUE;
			for(int j=i+1;j<in.length;j++){
				diff = (in[j] - in[i]);
				if(diff > 0 && diff < mindiff){
					mindiff = diff;
					num = in[j];
					check[i] = true;
				}				
			}
		
			if(check[i]){
				out[k++] = num;
			}else{
				out[k++] = in[i];
			}
		}	
		return out;
	}	
}

- silvimasss February 01, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test20_ArrayNextBiggerElement {
	public void addNextBigger(int[] arr) {
		int min = 0;
		for (int i = 0; i < arr.length; i++) {
			min = Integer.MAX_VALUE;
			for (int j = i + 1; j < arr.length; j++)
				if (arr[j] > arr[i])
					min = Math.min(min, arr[j]);
			if (min != Integer.MAX_VALUE)
				arr[i] = min;
		}
	}

	public static void main(String[] args) {
		int[] arr = { 2, 5, -9, 6, 3, -4, 8, 1, 15, 12 };
		Test20_ArrayNextBiggerElement obj = new Test20_ArrayNextBiggerElement();
		obj.addNextBigger(arr);
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}

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

public class Test20_ArrayNextBiggerElement {
	public void addNextBigger(int[] arr) {
		int min = 0;
		for (int i = 0; i < arr.length; i++) {
			min = Integer.MAX_VALUE;
			for (int j = i + 1; j < arr.length; j++)
				if (arr[j] > arr[i])
					min = Math.min(min, arr[j]);
			if (min != Integer.MAX_VALUE)
				arr[i] = min;
		}
	}

	public static void main(String[] args) {
		int[] arr = { 2, 5, -9, 6, 3, -4, 8, 1, 15, 12 };
		Test20_ArrayNextBiggerElement obj = new Test20_ArrayNextBiggerElement();
		obj.addNextBigger(arr);
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}

- Nishant March 14, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test20_ArrayNextBiggerElement {
	public void addNextBigger(int[] arr) {
		int min = 0;
		for (int i = 0; i < arr.length; i++) {
			min = Integer.MAX_VALUE;
			for (int j = i + 1; j < arr.length; j++)
				if (arr[j] > arr[i])
					min = Math.min(min, arr[j]);
			if (min != Integer.MAX_VALUE)
				arr[i] = min;
		}
	}

	public static void main(String[] args) {
		int[] arr = { 2, 5, -9, 6, 3, -4, 8, 1, 15, 12 };
		Test20_ArrayNextBiggerElement obj = new Test20_ArrayNextBiggerElement();
		obj.addNextBigger(arr);
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}

- nishu.nitk March 14, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List<Integer>  li = new ArrayList();
    Integer[] arr = {2,5,9,6,3,4,8,15,12};
    li=  Arrays.asList(arr);
    List<Integer>  li2 =  new ArrayList();
    for(int i=0;i<arr.length;i++) {
      li2.add(arr[i]);
    }   
    System.out.println(li2);
    List<Integer>  li3 = new ArrayList();
    List<Integer>  li4 = new ArrayList();
    for(int i=0;i<li.size();i++) {
         li2.remove(li2.indexOf(li.get(i)));
        li3.clear();
        li3.addAll(li2);
        Collections.sort(li3);
        if(li3.isEmpty()) {
            li4.add(li.get(i));
        }
        for(int j=0;j<li3.size();j++) {
                if(li.get(i) < li3.get(j))  {
                    li4.add(li3.get(j));
                    break;
                }
            
                if(j==li3.size()-1) {
                    li4.add(li.get(i));
                }
    }
    }
    System.out.println(li4);

- Monisha March 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List<Integer>  li = new ArrayList();
    Integer[] arr = {2,5,9,6,3,4,8,15,12};
    li=  Arrays.asList(arr);
    List<Integer>  li2 =  new ArrayList();
    for(int i=0;i<arr.length;i++) {
      li2.add(arr[i]);
    }   
    System.out.println(li2);
    List<Integer>  li3 = new ArrayList();
    List<Integer>  li4 = new ArrayList();
    for(int i=0;i<li.size();i++) {
         li2.remove(li2.indexOf(li.get(i)));
        li3.clear();
        li3.addAll(li2);
        Collections.sort(li3);
        if(li3.isEmpty()) {
            li4.add(li.get(i));
        }
        for(int j=0;j<li3.size();j++) {
                if(li.get(i) < li3.get(j))  {
                    li4.add(li3.get(j));
                    break;
                }
            
                if(j==li3.size()-1) {
                    li4.add(li.get(i));
                }
    }
    }
    System.out.println(li4);

- MONISHA March 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class NextBigIntArray{
public static void updateArray(int[] arr){
int d, cd, nextInt;

for(int i=0;i<arr.length;++i){
d=0;
nextInt=arr[i];
for(int j=i+1;j<arr.length;++j){
cd=arr[j]-arr[i];

if(0<cd && (0==d || d>cd)){
nextInt=arr[j];
d=cd;
}
}
arr[i]=nextInt;
}
}

public static void main(String[] args){
int[] arr = {8, 7, 9, 1, 10, 12, 5, 4};
// int[] arr = {-6, -3, -1, -2};

updateArray(arr);

for(int i:arr)
System.out.print(i);
}
}

- Simon March 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class NextBigIntArray{
	public static void updateArray(int[] arr){
		int d, cd, nextInt;

		for(int i=0;i<arr.length;++i){
			d=0;
			nextInt=arr[i];
			for(int j=i+1;j<arr.length;++j){
				cd=arr[j]-arr[i];

				if(0<cd && (0==d || d>cd)){
					nextInt=arr[j];
					d=cd;
				}
			}
			arr[i]=nextInt;
		}
	}

	public static void main(String[] args){
		int[] arr = {8, 7, 9, 1, 10, 12, 5, 4};
		// int[] arr = {-6, -3, -1, -2};

		updateArray(arr);

		for(int i:arr)
			System.out.print(i);
	}
}

- Simon March 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think its time and space complexity it O(n)

void nextgreat(vector<int>& nums){
    set<int> my_set;
    set<int>:: iterator it;
    int i = nums.size()-1;
    while(i > -1){
        my_set.insert(nums[i]);
        it = my_set.find(nums[i]);
        ++it;
        if(it != my_set.end()) nums[i] = (*it);
        i--;
    }
}

- NoName May 29, 2019 | 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