Published: 26 Oct 2024
Reading Time: 6 min read
Preparing for an Oracle coding interview is crucial for anyone looking to work with Oracle databases or in software development roles involving Oracle technologies. In the coding round, you may face questions that test your knowledge of SQL, PL/SQL, and Java. Understanding how to write efficient queries, create PL/SQL procedures, and integrate Java with Oracle databases is essential.
This guide will cover key concepts with sample Oracle coding questions and answers and useful tips for success. Whether you are a beginner or someone with experience, this resource will help you build confidence to improve your skills and prepare well for upcoming interviews.
Here are the key topics you need to focus on for the Oracle coding assessment in 2024:
Here are the sample Oracle coding round questions with solutions:
Problem Statement:
Ratan is a wealthy investor who wants to maximize his profits by buying and selling stocks. Given the stock prices for several days, calculate the maximum profit he could earn.
Input Format:
Output Format:
Example:
Input
7
1
9
2
11
1
9
2
Output
10
Solution in C:
#include <stdio.h>
int main() {
int n, profit = 0, min_price = 1e9;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int price;
scanf("%d", &price);
if (price < min_price) min_price = price;
profit = (price - min_price > profit) ? price - min_price : profit;
}
printf("%d\n", profit);
return 0;
}
Solution in Java:
import java.util.Scanner;
public class MaxProfit {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int profit = 0, minPrice = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
int price = scanner.nextInt();
minPrice = Math.min(minPrice, price);
profit = Math.max(profit, price - minPrice);
}
System.out.println(profit);
}
}
Solution in Python:
n = int(input())
profit = 0
min_price = float('inf')
for _ in range(n):
price = int(input())
min_price = min(min_price, price)
profit = max(profit, price - min_price)
print(profit)
Problem Statement:
You have received loan offers from two banks with varying interest rates over different tenures. Calculate the total interest paid for each bank and decide which offer is cheaper.
Input Format:
Output Format:
Example:
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
Solution in C:
#include <stdio.h>
#include <math.h>
double calculate_total_emi(double p, int years, int slabs) {
double total = 0;
for (int i = 0; i < slabs; i++) {
int period;
double rate;
scanf("%d %lf", &period, &rate);
double monthlyRate = rate / 1200; // converting to monthly
double emi = (p * monthlyRate) / (1 - pow(1 + monthlyRate, -period * 12));
total += emi * period * 12; // total interest paid
}
return total;
}
int main() {
double P;
int T, N1, N2;
scanf("%lf %d", &P, &T);
scanf("%d", &N1);
double bankA_total = calculate_total_emi(P, T, N1);
scanf("%d", &N2);
double bankB_total = calculate_total_emi(P, T, N2);
if (bankA_total < bankB_total) printf("Bank A\n");
else printf("Bank B\n");
return 0;
}
Solution in Java:
import java.util.Scanner;
public class BestBank {
static double calculateTotalEMI(double principal, int slabs, Scanner scanner) {
double total = 0;
for (int i = 0; i < slabs; i++) {
int period;
double rate;
period = scanner.nextInt();
rate = scanner.nextDouble();
double monthlyRate = rate / 1200; // converting to monthly
double emi = (principal * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -period * 12));
total += emi * period * 12; // total interest paid
}
return total;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double P = scanner.nextDouble();
int T = scanner.nextInt();
int N1 = scanner.nextInt();
double bankA_total = calculateTotalEMI(P, N1, scanner);
int N2 = scanner.nextInt();
double bankB_total = calculateTotalEMI(P, N2, scanner);
System.out.println(bankA_total < bankB_total ? "Bank A" : "Bank B");
}
}
Problem Statement:
Determine how many borrow operations are needed to subtract two numbers, or return "Not possible" if the subtraction can't be performed.
Input Format:
Output Format:
Example:
Input
754
658
Output
2
Solution in C:
#include <stdio.h>
#include <string.h>
int countBorrows(char* num1, char* num2) {
int len1 = strlen(num1);
int len2 = strlen(num2);
if (len1 < len2 || (len1 == len2 && strcmp(num1, num2) < 0)) {
return -1;
}
int borrow = 0, count = 0;
for (int i = 0; i < len1; i++) {
int digit1 = num1[len1 - 1 - i] - '0';
int digit2 = (i < len2) ? num2[len2 - 1 - i] - '0' : 0;
if (digit1 < digit2 + borrow) {
borrow = 1;
count++;
} else {
borrow = 0;
}
}
return count;
}
int main() {
char num1[100], num2[100];
scanf("%s %s", num1, num2);
int result = countBorrows(num1, num2);
if (result == -1) {
printf("Not possible\n");
} else {
printf("%d\n", result);
}
return 0;
}
Solution in Java:
import java.util.Scanner;
public class BorrowNumber {
public static int countBorrows(String num1, String num2) {
if (num1.length() < num2.length() || (num1.length() == num2.length() && num1.compareTo(num2) < 0)) {
return -1;
}
int borrow = 0, count = 0;
for (int i = 0; i < num1.length(); i++) {
int digit1 = num1.charAt(num1.length() - 1 - i) - '0';
int digit2 = (i < num2.length()) ? num2.charAt(num2.length() - 1 - i) - '0' : 0;
if (digit1 < digit2 + borrow) {
borrow = 1;
count++;
} else {
borrow = 0;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num1 = sc.next();
String num2 = sc.next();
int result = countBorrows(num1, num2);
if (result == -1) {
System.out.println("Not possible");
} else {
System.out.println(result);
}
}
}
Solution in Python:
def count_borrows(num1, num2):
if len(num1) < len(num2) or (len(num1) == len(num2) and num1 < num2):
return -1
borrow = 0
count = 0
for i in range(len(num1)):
digit1 = int(num1[-1 - i])
digit2 = int(num2[-1 - i]) if i < len(num2) else 0
if digit1 < digit2 + borrow:
borrow = 1
count += 1
else:
borrow = 0
return count
num1 = input().strip()
num2 = input().strip()
result = count_borrows(num1, num2)
if result == -1:
print("Not possible")
else:
print(result)
Problem Statement:
Given an array of integers, count the number of distinct elements in a specified range.
Input Format:
Output Format:
Example:
Input
5
1 2 2 3 4
1 3
Output
3
Solution in C:
#include <stdio.h>
#include <stdlib.h>
int distinctCount(int* arr, int l, int r) {
int hash[1000] = {0}; // Adjust size as necessary
for (int i = l; i <= r; i++) {
hash[arr[i]] = 1;
}
int count = 0;
for (int i = 0; i < 1000; i++) {
if (hash[i] == 1) count++;
}
return count;
}
int main() {
int n, l, r;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d %d", &l, &r);
printf("%d\n", distinctCount(arr, l, r));
return 0;
}
Solution in Java:
import java.util.HashSet;
import java.util.Scanner;
public class DistinctElements {
public static int distinctCount(int[] arr, int l, int r) {
HashSet<Integer> distinct = new HashSet<>();
for (int i = l; i <= r; i++) {
distinct.add(arr[i]);
}
return distinct.size();
}
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 l = sc.nextInt();
int r = sc.nextInt();
System.out.println(distinctCount(arr, l, r));
}
}
Solution in Python:
def distinct_count(arr, l, r):
return len(set(arr[l:r+1]))
n = int(input())
arr = list(map(int, input().split()))
l, r = map(int, input().split())
print(distinct_count(arr, l, r))
Problem Statement:
Given an array consisting of colors represented as integers, sort the array so that all occurrences of color 0 come first, followed by 1s, and then 2s.
Input Format:
Output Format:
Example:
Input
5
2 0 1 2 0
Output
0 0 1 2 2
Solution in C:
#include <stdio.h>
void sortColors(int* arr, int n) {
int count[3] = {0};
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
for (int i = 0; i < count[0]; i++) arr[i] = 0;
for (int i = 0; i < count[1]; i++) arr[count[0] + i] = 1;
for (int i = 0; i < count[2]; i++) arr[count[0] + count[1] + i] = 2;
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
sortColors(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Solution in Java:
import java.util.Scanner;
public class SortColors {
public static void sortColors(int[] arr) {
int[] count = new int[3];
for (int num : arr) {
count[num]++;
}
for (int i = 0; i < count[0]; i++) arr[i] = 0;
for (int i = 0; i < count[1]; i++) arr[count[0] + i] = 1;
for (int i = 0; i < count[2]; i++) arr[count[0] + count[1] + i] = 2;
}
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();
}
sortColors(arr);
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
Solution in Python:
def sort_colors(arr):
count = [0, 0, 0]
for num in arr:
count[num] += 1
index = 0
for i in range(3):
for _ in range(count[i]):
arr[index] = i
index += 1
n = int(input())
arr = list(map(int, input().split()))
sort_colors(arr)
print(" ".join(map(str, arr)))
Problem Statement:
Count the number of vowels in a given string.
Input Format:
Output Format:
Example:
Input
Hello World
Output
3
Solution in C:
#include <stdio.h>
#include <string.h>
int countVowels(char* str) {
int count = 0;
for (int i = 0; str[i]; i++) {
char c = str[i];
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
count++;
}
}
return count;
}
int main() {
char str[100];
fgets(str, sizeof(str), stdin);
printf("%d\n", countVowels(str));
return 0;
}
Solution in Java:
import java.util.Scanner;
public class CountVowels {
public static int countVowels(String str) {
int count = 0;
for (char c : str.toCharArray()) {
if ("aeiouAEIOU".indexOf(c) != -1) {
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(countVowels(str));
}
}
Solution in Python:
def count_vowels(s):
return sum(1 for char in s if char.lower() in 'aeiou')
s = input()
print(count_vowels(s))
Here are some essential tips for cracking Oracle coding questions:
Getting ready for Oracle coding questions takes time and effort, but with the right preparation, you can succeed. By understanding important concepts, practicing common questions, and using our tips, you'll be well-equipped for your Oracle coding interview.
Practice Java programming and learn about how Java works with Oracle databases using JDBC.
Look for online coding platforms, Oracle documentation, and coding challenge websites to practice.
Source: NxtWave - CCBP Blog
Original URL: https://www.ccbp.in/blog/articles/oracle-coding-questions