Oracle Coding Questions and Answers
Oracle Coding Questions with Solutions
Every year, Oracle searches for engineers to hire through On-Campus and Off-Campus Drives. Oracle Coding Questions and Answers Page will help you to get sample Coding Questions asked in the Online Assessment and Technical Interviews of Oracle.
Sample Oracle Coding Questions and Answers
Question 1:
Problem Statement :
Ratan is a crazy rich person. And he is blessed with luck, so he always made the best profit possible with the shares he bought. That means he bought a share at a low price and sold it at a high price to maximize his profit. Now you are an income tax officer and you need to calculate the profit he made with the given values of stock prices each day. You have to calculate only the maximum profit Ratan earned.
Note that:
Ratan never goes into loss.
Example 1 :
Price=[1,6,2]
Ratan buys it on the first day and sells it on the second.
Example 2 :
Price=[9,8,6]
The Price always went down, Ratan never bought it.
Input Format:
First line with an integer n, denoting the number days with the value of the stack
Next n days, telling the price of the stock on that very day.
Output Format:
Maximum profit done by Ratan in a single line.
Constraints:
Number of days <=10^8
Sample Input for Custom Testing:
STDIN
7
1
9
2
11
1
9
2
Sample Output :
10
Explanation :
The maximum profit possible is when Ratan buys it in 1 rupees and sells it in 11.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
#include <bits/stdc++.h> using namespace std; int solve(vector < int > v) { int n = v.size(); if (n == 0) return 0; int mx = v[0]; for (int i = 1; i < n; i++) mx = max(mx, v[i]); if (mx <= 0) return 0; int mxSum = 0; int cSum = 0; for (int i = 0; i < n; i++) { cSum += v[i]; if (cSum < 0) cSum = 0; mxSum = max(mxSum, cSum); } return mxSum; } int main() { int n; cin >> n; int price[n]; for (int i = 0; i < n; i++) cin >> price[i]; vector < int > diff; for (int i = n-2; i >= 0 ; i--) diff.push_back(price[i+1] - price[i]); int ans = solve(diff); if(ans < 0) cout << 0 << endl; else cout<< ans << endl; }
import java.util.*; public class PrepInsta { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int price[] = new int[n]; for(int i=0;i < n;i++) { price[i] = sc.nextInt(); } Vector < Integer > diff = new Vector<>(); for(int i=n-2;i >=0;i--) { diff.add(price[i+1]-price[i]); } int ans = solve(diff); if(ans < 0) { System.out.println(0); }else { System.out.println(ans); } } private static int solve(Vector < Integer > v) { int n = v.size(); if(n==0) { return 0; } int mx = v.get(0); for(int i=1;i < n;i++) { mx = Math.max(mx, v.get(i)); } if(mx <= 0) { return 0; } int mxSum=0,csum=0; for(int i=0;i < n;i++) { csum+=v.get(i); if(csum < 0) csum=0; mxSum = Math.max(csum, mxSum); } return mxSum; } }
def func(diff): n=len(diff) if n==0: return 0 mx=max(diff) if mx <= 0: return 0 mxS=0 cS=0 for i in diff: cS+=i if cS <= 0: cS=0 mxS=max(cS,mxS) return mxS n=int(input()) arr=[] diff=[] ans=[0] for i in range(n): arr.append(int(input()))c++ for i in range(n-1): diff.append(arr[i+1]-arr[i]) ans=func(diff) if ans < 0: print("0") else: print(ans)
Question 2:
Problem Description
Question – : There are two banks – Bank A and Bank B. Their interest rates vary. You have received offers from both banks in terms of the annual rate of interest, tenure, and variations of the rate of interest over the entire tenure.You have to choose the offer which costs you least interest and reject the other. Do the computation and make a wise choice.
The loan repayment happens at a monthly frequency and Equated Monthly Installment (EMI) is calculated using the formula given below :
EMI = loanAmount * monthlyInterestRate / ( 1 – 1 / (1 + monthlyInterestRate)^(numberOfYears * 12))
Constraints:
- 1 <= P <= 1000000
- 1 <=T <= 50
- 1<= N1 <= 30
- 1<= N2 <= 30
Input Format:
- First line: P principal (Loan Amount)
- Second line: T Total Tenure (in years).
- Third Line: N1 is the number of slabs of interest rates for a given period by Bank A. First slab starts from the first year and the second slab starts from the end of the first slab and so on.
- Next N1 line will contain the interest rate and their period.
- After N1 lines we will receive N2 viz. the number of slabs offered by the second bank.
- Next N2 lines are the number of slabs of interest rates for a given period by Bank B. The first slab starts from the first year and the second slab starts from the end of the first slab and so on.
- The period and rate will be delimited by single white space.
Output Format: Your decision either Bank A or Bank B.
Explanation:
- Example 1
- Input
- 10000
- 20
- 3
- 5 9.5
- 10 9.6
- 5 8.5
- 3
- 10 6.9
- 5 8.5
- 5 7.9
- Output: Bank B
- Example 2
- Input
- 500000
- 26
- 3
- 13 9.5
- 3 6.9
- 10 5.6
- 3
- 14 8.5
- 6 7.4
- 6 9.6
- Output: Bank A
#include<stdio.h> #include<string.h> int main() { double p,s,mi,sum,emi,bank[5],sq; int y,n,k,i,yrs,l=0; scanf(" %lf",&p); scanf(" %d",&y); for(k=0;k<2;k++) { scanf(" %d",&n); sum=0; for(i=0;i<n;i++) { scanf(" %d",&yrs); scanf(" %lf",&s); mi=0; sq=pow((1+s),yrs*12); emi= (p*(s))/(1-1/sq); sum= sum + emi; }bank[l++]=sum; } if(bank[0]<bank[1]) printf(" Bank A "); else printf(" Bank B "); return 0; }
Output 10000 20 3 5 9.5 10 9.6 5 8.5 3 10 6.9 5 8.5 5 7.9 Bank B
#include<bits/stdc++.h> using namespace std; int main() { double p,s,mi,sum,emi,bank[5],sq; int y,n,k,i,yrs,l=0; cin>>p; cin>>y; for(k=0;k<2;k++) { cin>>n; sum=0; for(i=0;i<n;i++) { cin>>yrs; cin>>s; mi=0; sq=pow((1+s),yrs*12); emi= (p*(s))/(1-1/sq); sum= sum + emi; } bank[l++]=sum; } if(bank[0]<bank[1]) cout<<("Bank A"); else cout<<("Bank B"); return 0; }
Output 10000 20 3 5 9.5 10 9.6 5 8.5 3 10 6.9 5 8.5 5 7.9 Bank B
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); double p,s,mi,sum,emi,sq; int y,n,k,yrs,l=0; double[] bank = new double[5]; System.out.println("Enter the principal amount"); p = sc.nextDouble(); System.out.println("Enter tenature year"); y = sc.nextInt(); for (k = 0; k < 2; k++) { System.out.println("Enter the no of slabs"); n = sc.nextInt(); sum=0; for (int i = 0; i < n; i++) { System.out.println("Enter the period :"); yrs = sc.nextInt(); System.out.println("Enter the interest :"); s = sc.nextDouble(); mi=0; sq=Math.pow((1+s), yrs*12); emi=(p*(s))/(1-1/sq); sum=sum+emi; } bank[l++]=sum; } if(bank[0]<bank[1]) System.out.println("Bank A"); else System.out.println("Bank B"); } }
Output 10000 20 3 5 9.5 10 9.6 5 8.5 3 10 6.9 5 8.5 5 7.9 Bank B
bank = [] principal = int(input()) year = int(input() for i in range(0, 2): # 2 Banks installments = int(input()) sum = 0 for i in range(0, installments): time, roi = [float(i) for i in input().split()] square = pow((1+roi), time*12) emi = (principal*(roi)/(1-1/square)) sum = sum + emi bank.append(sum) if bank[0] < bank[1]: print("Bank A") else: print("Bank B")
Output: 10000 20 3 5 9.5 10 9.6 5 8.5 3 10 6.9 5 8.5 5 7.9 Bank B
Question 3 : Network Stream
Problem Statement :
A stream of n data packets arrives at a server. This server can only process packets that are exactly 2^n units long for some non-negative integer value of n (0<=n).
All packets are repackaged in order to the 1 largest possible value of 2^n units. The remaining portion of the packet is added to the next arriving packet before it is repackaged. Find the size of the largest repackaged packet in the given stream.
Example arriving Packets = [12, 25, 10, 7, 8]
The first packet has 12 units. The maximum value of 2^n that can be made has 2^n = 2^3 = 8 units because the next size up is 2^n = 2^4 = 16 (16 is greater than 12).
12 – 8 = 4 units are added to the next packet. There are 4 + 25 = 29 units to repackage, 2^n = 2^4 = 16 is the new size leaving 9 units (29-16 = 9)
Next packet is 9 + 10 = 29 unists & the maximum units(in 2^n) is 16 leaving 3 units.
3 + 7 = 10 , the max units is 8 Leaving 2 units, and so on.
The maximum repackaged size is 16 units.
Returns:
Long : the size of the largest packet that is streamed
Constraints :
1<=n<=10^5
1<=arriving Packets[i] size<=10^9
Sample input 0:
5 → number of packets=5
13→ size of packets=[13,25,12,2,8]
25
10
2
8
Sample output 0:
16
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int temp = 1, maxVal = INT_MIN; for (int i = 0; i < n - 1; i++) { temp = 1; while (2 * temp <= arr[i]) temp = temp * 2; maxVal = max(temp, maxVal); arr[i + 1] += arr[i] - temp; } temp = 1; while (2 * temp <= arr[n - 1]) temp = temp * 2; maxVal = max(temp, maxVal); cout << maxVal << endl; return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); int temp = 1, max = Integer.MIN_VALUE; for (int i = 0; i < n - 1; i++) { temp = 1; while (2 * temp <= arr[i]) temp = temp * 2; max = Math.max(temp, max); arr[i + 1] += arr[i] - temp; } temp = 1; while (2 * temp <= arr[n - 1]) temp = temp * 2; max = Math.max(temp, max); System.out.println(max); } }
def largeRepackagedPacket(arr): twoP = [int(2**i) for i in range(31)] x = 0 ans = 0 for i in arr: i = i + x for j in range(31): if i < twoP[j]: break x = i - twoP[j - 1] if ans <= twoP[j - 1]: ans = twoP[j - 1] return ans Packets = [] for i in range(int(input())): Packets.append(int(input())) print(largeRepackagedPacket(Packets))
Question 4 :Borrow Number
Problem Statement :
You have two numbers number1 and number2, your job is to check the number of borrow operations needed for subtraction of number1 from number2. If the subtraction is not possible then return the string not possible.
Example :
754
658
Answer :
2
654
666
Answer :
Not possible
#include<bits/stdc++.h> using namespace std; int main() { string s1, s2; int c = 0, f = 0; cin >> s1 >> s2; if (stoi(s1) < stoi(s2)) { cout << "Impossible"; } reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); for (int i = 0; i < s1.length(); i++) if (s1[i] < s2[i]) { f = 1; c++; } else if (s1[i] == s2[i]) { if (f == 1) { c++; } f = 0; } else f = 0; cout << c; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int number1 = sc.nextInt(); int number2 = sc.nextInt(); int count = 0; if (number1 < number2) { System.out.println("Not possible"); } else { boolean flag = false; while (number1 != 0 && number2 != 0) { int temp1 = 0; int temp2 = number2 % 10; if (flag) temp1 = number1 % 10 - 1; else temp1 = number1 % 10; if (temp1 < temp2) { count++; flag = true; } else flag = false; number1 = number1 / 10; number2 = number2 / 10; } System.out.println(count); } } }
number1 = int(input()) number2 = int(input()) count = 0 if number1 < number2: print("Not possible") else: flag = 0 while number1 != 0 and number2 != 0: temp1 = 0 temp2 = number2 % 10 if flag: temp1 = number1 % 10 - 1 else: temp1 = number1 % 10 if temp1 < temp2: count += 1 flag = 1 else: flag = 0 number1 = number1 // 10 number2 = number2 // 10 print(count)
Question 5:
Rahul has an array a, consisting of n integers a1, a2, …, an. The boy cannot sit and do nothing, he decided to study an array. Rahul took a piece of paper and wrote out m integers l1, l2, …, lm (1 ≤ li ≤ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, …, n. Formally, he want to find the number of distinct numbers among ali, ali + 1, …, an.?
Rahul wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
- The first line contains two integers n and m (1 ≤ n, m ≤ 105). The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 105) — the array elements.
- Next m lines contain integers l1, l2, …, lm. The i-th line contains integer li (1 ≤ li ≤ n).
Output
- Print m lines — on the i-th line print the answer to the number li.
Examples
- Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10 - Output
6
6
6
6
6
5
4
3
2
1
n,m=map(int,input().split()) ar=list(map(int,input().split())) a=set() l=[0]*n for i in range(n-1,-1,-1): a.add(ar[i]) l[i]=len(a) #print(l) for i in range(m): lx=int(input()) print(l[lx-1])
#include<bits/stdc++.h> using namespace std; int main () { int n, m; cin >> n >> m; int a[n], b[m]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; set < int >st; int check[n] = { 0 }; for (int i = n - 1; i >= 0; i--) { st.insert (a[i]); check[i] = st.size (); } for (int i = 0; i < m; i++) cout << check[b[i] - 1] << endl; return 0; }
import java.util.*; class Solution { public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int m = sc.nextInt (); int a[] = new int[n]; int l[] = new int[m]; for (int i = 0; i < n; i++) a[i] = sc.nextInt (); for (int j = 0; j < m; j++) l[j] = sc.nextInt (); int dp[] = new int[n]; HashSet < Integer > s = new HashSet < Integer > (); for (int i = n - 1; i >= 0; i--) { s.add (a[i]); dp[i] = s.size (); } for (int j = 0; j < m; j++) System.out.println (dp[l[j] - 1]); } }
Question 6 : Biggest Meatball
Problem Statement – Bhojon is a restaurant company and has started a new wing in a city. They have every type of cook except the meatball artist. They had fired their last cook because the sale of meatballs in their restaurant is really great, and they can’t afford to make meatballs again and again every time their stock gets empty. They have arranged a hiring program, where you can apply with their meatball.
They will add the meatball in their seekh (a queue) and everytime they cut the meatball they take it and cut it on the day’s quantity and then re-add the meatball in the seekh. You are the hiring manager there and you are going to say who is gonna be hired.
Day’s quantity means, on that very day the company sells only that kg of meatballs to every packet.
If someone has less than a day’s quantity, it will be counted as a sell.
Function Description:
- Complete the function with the following parameters:
Parameters:
Name | Type | Description |
N | Integer | How many people are participating in the hiring process. |
D | Integer | Day’s quantity, how many grams of meatball is being sold to every packet. |
Array[ ] | Integer array | Array of integers, the weight of meatballs everyone came with. |
Return:
- The ith person whose meat is served at last.
Constraints:
- 1 <= N <= 10^3
- 1 <= D <= 10^3
- 1 <= Array[i] <= 10^3
Input Format:
- First line contains N.
- Second line contains D.
- After that N lines contain The ith person’s meatball weight.
Output Format: The 1 based index of the man whose meatball is served at the last.
Sample Input 1:
4
2
7
8
9
3
Sample Output 1:
3
Explanation:
The seekh or meatball queue has [7 8 9 3] this distribution. At the first serving they will cut 2 kgs of meatball from the first meatball and add it to the last of the seekh, so after 1st time it is:
[8 9 3 5]
Then, it is: [9 3 5 6], [3 5 6 7], [5 6 7 1], [6 7 1 3], [7 1 3 4], [1 3 4 5], [3 4 5], [4 5 1], [5 1 2], [1 2 3], [2 3], [3], [1], [0]
So the last served meatball belongs to the 3rd person.
#include<bits/stdc++.h> using namespace std; int main() { int n,x,a,m=0,maxPos=0; cin>>n; vector v(n); cin>>x; for(int i=0;i<n;i++){ cin>>v[i]; } for(int i=0;i<n;i++){ v[i]=(v[i]-1)/x; } for(int i=0;i<n;i++){ if(v[i]>=m){ m=v[i]; maxPos=i; } } cout<<maxPos+1; }
n=int(input()) m=0 mxPos=0 v=[0]*n x=int(input()) for i in range(n): v[i]=int(input()) for i in range(n): v[i]=(v[i]-1)//x for i in range(n): if v[i]>=m: m=v[i] mxPos=i print(mxPos+1)
import java.util.*; class Main { public static void main(String args[]) { Scanner sc = new Scanner (System.in); int n=sc.nextInt(); int v[]= new int[n]; int x=sc.nextInt(); for(int i=0; i<n; i++) v[i]=sc.nextInt(); for(int i=0;i<n;i++){ v[i]=(v[i]-1)/x; } int m=0, maxPos=0; for(int i=0;i<n;i++){ if(v[i]>=m){ m=v[i]; maxPos=i; } } System.out.println(maxPos+1); } }
Question 7 : Street Light
Street Lights are installed at every position along a 1-D road of length n.
Locations[] (an array) represents the coverage limit of these lights. The ith light has a coverage limit of locations[i] that can range from the position max((i – locations[i]), 1) to min((i + locations[i]), n ) (Closed intervals). Initially all the lights are switched off. Find the minimum number of fountains that must be switched on to cover the road.
Example
n = 3
locations[] = {0, 2, 13}then
For position 1: locations[1] = 0, max((1 – 0),
1) to mini (1+0), 3) gives range = 1 to 1
For position 2: locations[2] = 2, max((2-2),
1) to min( (2+2), 3) gives range = 1 to 3
For position 3: locations[3] = 1, max( (3-1),
1) to min( (3+1), 3) gives range = 2 to 3
For the entire length of this road to be covered, only the light at position 2 needs to be activated.
Function Description
Returns
int: the minimum number of street lights that must be activated
Constraints
- 1<_n<_ 10^5
- O<_locations[i] <_ mini (n,100) (where 1 <_1<_
10^5)
► Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing
3 ->locations[] size n = 3
1 ->locations[] [1, 1, 1]
1 ->Sample Output
Sample Output
1
#include<bits/stdc++.h> #define ll long long using namespace std; bool compare(pair<int, int> A, pair<int, int> B) { if (A.first = B.first) return A.second < B.second; return A.first < B.first; } int solve(int location[], int n) { pair<int, int> range[n]; for (int i = 0; i < n; i++) { int id = i + 1; range[i].first = max(1, id - location[i]); range[i].second = min(n, id + location[i]); } sort(range, range + n, compare); int i = 0; int ans = 0; while (i < n) { pair<int, int> p = range[i]; ans++; while (i + 1 < n && range[i].first == range[i + 1].first) { p.second = max(p.second, range[i + 1].second); i++; } //cout<<p.second<<" "<<i<<endl; while (i < n && range[i].second <= p.second) i++; //cout<<p.second<<" "<<i<<endl; } return ans; } int main() { int n; cin >> n; int location[n]; for (int i = 0; i < n; i++) cin >> location[i]; cout << solve(location, n) << endl; return 0; }
Question 8:
Problem Description
A 10cm x 10cm x 10cm solid cube rests on the ground. It has a beetle on it, as well as some sweet honey spots on the cube’s surface. The beetle begins at a point on the cube’s surface and moves in a clockwise direction along the cube’s surface to the honey spots.
- If it goes from one point on the same face to another (say, X to Y), it goes in an arc of a circle that subtends an angle of 60 degrees at the circle’s center.
- If it travels from one point to another on a different face, it takes the shortest path on the cube’s surface, except that it never travels along its bottom.
The beetle is a Cartesian geometry student who knows the coordinates (x, y, z) of all the points it needs to visit. Its coordinate origin is one of the cube’s corners on the ground, and the z-axis points up. As a result, z=0 is the bottom surface (on which it does not crawl), and z=10 is the top. The beetle keeps track of all distances traveled and rounded the distance to two decimal places when it arrives at the following location so that the final distance is a sum of the rounded distances from spot to spot.
Input
The first line returns an integer N, the total number of points visited by the beetle (including the starting point).
The second line contains 3N non-negative numbers, each with two decimal places. These are to be interpreted as the x, y, and z coordinates of the points the beetle must visit in the given order.
Output
One line containing a number representing the total distance traveled by the beetle to two decimal places. Even if the travel distance is an integer, the output should have two decimal places.
Constraints
None of the points visited by the beetle are on the bottom face (z=0) or on any of the cube’s edges (the lines where two faces meet)
2<=N<=10
Example
Input : 31 1 10 2 1 10 0 1 9
Output : 4.05
Explanation :The beetle visits three different locations (N=3). The beetle begins on the cube's top face (z=10) at point (1,1,10) and moves to another point on the same face (2,1,10). Although the straight line distance is one, it travels on the arc of a circle with an angle of 60 degrees at its center and thus travels (2*pi)/6 or 1.05 (note that it rounds the distance at each leg of the journey). It moves along the cube's surface from (2,1,10) on the face z=10 to (0,1,9) on the face x=0. This is a three-mile distance. The total travel distance is 1.05+3=4.05. The result is 4.05
#include<stdio.h> #include<string.h> #define PIE 3.14 int s_x,s_y,s_z; float shortDist(int x, int y, int z) { float dis; // check if the Z-axis and any other one axis are the same. if(z == s_z && ( y == s_y || x == s_x) && s_z != 0) { //check if the x axis of next co-ordinate is same if(x != s_x) dis = (2 * PIE * (abs(x-s_x)))/6.0; //check if the y axis of next co-ordinate is same else dis = (2 * PIE * (abs(y-s_y)))/6.0; } // finddistance between x and y and the abs distance of Z axis else dis = (int)(sqrt(pow(x-s_x,2) + pow(y-s_y,2)) + abs(z-s_z)); s_x = x; s_y = y; s_z = z; return dis; } int main() { int n; scanf("%d", &n); n = 3 * n; int arr[n]; for(int i = 0;i < n;i++) scanf("%d", &arr[i]); float sum = 0.0; s_x = arr[0]; s_y = arr[1]; s_z = arr[2]; for(int i=3;i<n;i+=3) { sum += shortDist(arr[i],arr[i+1],arr[i+2]); } printf("%.2f",sum); }
#include<bits/stdc++.h> using namespace std; #define PIE 3.14 int s_x,s_y,s_z; float shortDist(int x, int y, int z) { float dis; // check if the Z-axis and any other one axis are the same. if(z == s_z && ( y == s_y || x == s_x) && s_z != 0) { //check if the x axis of next co-ordinate is same if(x != s_x) dis = (2 * PIE * (abs(x-s_x)))/6.0; //check if the y axis of next co-ordinate is same else dis = (2 * PIE * (abs(y-s_y)))/6.0; } // finddistance between x and y and the abs distance of Z axis else dis = (int)(sqrt(pow(x-s_x,2) + pow(y-s_y,2)) + abs(z-s_z)); s_x = x; s_y = y; s_z = z; return dis; } int main() { int n; cin>>n; n = 3 * n; int arr[n]; for(int i = 0;i < n;i++) cin>>arr[i]; float sum = 0.0; s_x = arr[0]; s_y = arr[1]; s_z = arr[2]; for(int i=3;i<n;i+=3) { sum += shortDist(arr[i],arr[i+1],arr[i+2]); } printf("%.2f",sum); }
import java.util.*; import java.lang.*; import java.io.*; class Main{ final static float PIE = 3.14f; static int s_x, s_y, s_z; public static void main(String[] args) { int i, N, x, y, z; int arr[] = new int[50]; float sum = 0.0f; Scanner sc = new Scanner(System.in); N = sc.nextInt(); N = 3 * N; for(i = 0; i < N; i++){ arr[i] = sc.nextInt(); } s_x = arr[0]; s_y = arr[1]; s_z = arr[2]; for(i = 3; i < N ; i += 3){ sum += shortDist(arr[i], arr[i+1], arr[i+2]); } System.out.printf("%.2f", sum); } private static float shortDist(int x, int y, int z) { float dis; // check if the Z-axis and any other one axis are the same. if(z == s_z && (y == s_y || x == s_x ) && s_z != 0){ //check if the x axis of next co-ordinate is same if(x != s_x){ dis = (2 * PIE * (Math.abs(x - s_x))) / 6.0f; } //check if the y axis of next co-ordinate is same else{ dis = (2 * PIE * (Math.abs(y - s_y))) / 6.0f; } } else{ dis = (int)(Math.sqrt(Math.pow(x - s_x, 2) + Math.pow(y - s_y, 2)) + Math.abs(z - s_z)); } s_x = x; s_y = y; s_z = z; return dis; } }
import math PIE=3.14 def shortDist( x,y,z,sx,sy,sz): dis = 0.0 # if the x or y axis same as z axis if(z == s_z and (y == s_y or x==s_x) and s_z != 0): # if the x axis of next co-ordinate is same if(x != s_x): dis = (2 * PIE * (abs(x - s_x)))/6.0 # if the y axis of next co-ordinate is same else: dis = (2 * PIE * (abs(y -s_y)))/6.0 # else means bee is going to another face #finding eculidean distance else: dis = int((math.sqrt(pow(x-s_x, 2) + pow(y-s_y, 2)) + abs(z - s_z))) s_x = x s_y = y s_z = z return dist,s_x,s_y,s_z n=int(input()) arr = [int(x) for x in input().split()] s_x = arr[0] s_y = arr[1] s_z = arr[2] ans=0 for j in range(3,3*n,3): x,s_x,s_y,s_z= shortDist(arr[j],arr[j+1],arr[j+2],s_x,s_y,s_z) ans += x print(round(ans,2))
Question 9 : Guess the word
Problem Statement :
Kochouseph Chittilappilly went to Dhruv Zplanet , a gaming space, with his friends and played a game called “Guess the Word”.
Rules of games are –
Computer displays some strings on the screen and the player should pick one string / word if this word matches with the random word that the computer picks then the player is declared as Winner.
Kochouseph Chittilappilly’s friends played the game and no one won the game. This is Kochouseph Chittilappilly’s turn to play and he decided to must win the game.
What he observed from his friend’s game is that the computer is picking up the string whose length is odd and also that should be maximum. Due to system failure computers sometimes cannot generate odd length words. In such cases you will lose the game anyways and it displays “better luck next time”. He needs your help. Check below cases for better understand
Sample input 0:
5 → number of strings
Hello Good morning Welcome you
Sample output 0:
morning
Explanation:
Hello → 5
Good → 4
Morning → 7
Welcome → 7
You → 3
First word that is picked by computer is morning
Sample input 1:
3
Go to hell
Sample output 1:
Better luck next time
Explanation:
Here no word with odd length so computer confuses and gives better luck next time
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string a, result = ""; while (n--) { cin >> a; if (a.length() & 1) if (a.length() > result.length()) result = a; } if (result == "") cout << "Better luck next time"; else cout << result; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String arr[] = new String[n]; for (int i = 0; i < n; i++) arr[i] = sc.next(); int len = 0; ArrayList < String > oddLength = new ArrayList < String > (); for (int i = 0; i < n; i++) { len = arr[i].length(); if (len % 2 == 1) oddLength.add(arr[i]); } if (oddLength.size() == 0) System.out.println("Better luck next time"); else { Iterator itr = oddLength.iterator(); int max = -1; String res = ""; while (itr.hasNext()) { String temp = (String) itr.next(); if (temp.length() > max) { res = temp; max = temp.length(); } } System.out.println(res); } } }
n = int(input()) L = list(map(str, input().split())) result = "" for a in L: if len(a) & 1: if len(a) > len(result): result = a if result == "": result = "Better luck next time" print(result)
Question 10 : Array Subarray
Problem Statement :
You are given an array, You have to choose a contiguous subarray of length ‘k’, and find the minimum of that segment, return the maximum of those minimums.
Sample input 0 :
1 → Length of segment x =1
5 → size of space n = 5
1 → space = [ 1,2,3,1,2]
2
3
1
2
Sample output :
3
Explanation :
The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3
#include <bits/stdc++.h> using namespace std; vector < int > arr; int prevmin = -1; int flag = 0; int x, n, q; int sorting(int start, int end) { if (start + 1 == n) { start = 0; end = end - n; } if (start == end) return arr[start]; return min(arr[start], sorting(start + 1, end)); } int func(int start, int end) { if (flag == 0) { flag++; return prevmin = sorting(start, end); } if (arr[start - 1] == prevmin) return prevmin; return prevmin = (arr[end] <= prevmin) ? prevmin : sorting(start, end); } int main() { cin >> x >> n; int ans = 0; for (int i = 0; i < n; i++) { cin >> q; arr.push_back(q); } for (int i = 0; i < n; i++) { ans = max(ans, func(i, i + x - 1)); } cout << ans; }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i = 0; i <= n - x; i++) { min = Integer.MAX_VALUE; for (int j = i; j < (i + x); j++) min = Math.min(min, arr[j]); max = Math.max(min, max); } System.out.println(max); } }
s = int(input()) n = int(input()) a = [] for i in range(n): a.append(int(input())) def min_in_segment(start, end, prev, s, prev_min): if s == 1: return a[start] else: if prev == -1 or prev_min == -2: return min(a[start : end + 1]) elif prev_min != -2: if prev != prev_min: if a[end] < prev_min: return a[end] else: return prev_min else: return min(a[start : end + 1]) msf = -1 prev = -1 prev_min = -2 for i in range(n - s + 1): new_min = min_in_segment(i, i + s - 1, prev, s, prev_min) msf = max(msf, new_min) prev = a[i] prev_min = new_min print(msf)
FAQs related to Oracle Coding Questions
Question 1: Is Coding questions asked in Oracle Recruitment Process?
Yes, In Online Assessment and Technical Interviews Oracle asks DSA Based Coding Questions.
Question 2: How long is Oracle recruitment process?
Oracle usually takes 6 – 8 Weeks to complete whole Recruitment Process.
Question 3: What is Oracle interview process?
Before Technical Interview there will be a Group Discussion, conducted by Oracle for those applicants who have successfully passed Online Aptitude & Coding Assessments.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others