Revature Previous Coding Questions
🔥 Revature Previous Coding Questions with Java & Python Solutions
Welcome to this specially curated post featuring Revature Previous Coding Questions.
Each problem includes:
- A clear problem statement
- Java and Python solutions
- Example input/output
- Concise and clear explanation
Let’s get started! 🚀
✅ Problem 1: Sum of Arithmetic Progression
📝 Problem Statement
You are given an arithmetic progression (AP) with N terms.
Your task is to calculate the sum of the series.
You may derive the first term A and common difference D from the input.
📐 Formula Used
Sum = (N / 2) × (2A + (N – 1) × D)
🔢 Input
- Integer N — number of terms
- N space-separated integers forming the AP
🧾 Output
- A single integer — the sum of the AP
✅ Example
Input:
5
2 4 6 8 10
Output:
30
🔣Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; public class APSum { 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 A = arr[0]; int D = arr[1] - arr[0]; int sum = (N * (2 * A + (N - 1) * D)) / 2; System.out.println(sum); } } |
1 2 3 4 5 6 7 8 |
N = int(input()) arr = list(map(int, input().split())) A = arr[0] D = arr[1] - arr[0] sum_ap = (N * (2 * A + (N - 1) * D)) // 2 print(sum_ap) |
🔍 Explanation
We extract the first element A and compute the common difference D.
Using the arithmetic progression sum formula, we calculate and print the total.
✅ Problem 2: Pointer Conversion in Code
📝 Problem Statement
Given C-style code, convert expressions like a*b into a->b,
except on lines that start with comments (//).
🔢 Input
- Up to 100 lines of C-style code
- Each line ≤ 50 characters
🧾 Output
- Modified code with * replaced by -> where appropriate
✅ Example
Input:
// ignore this line
a*b = 5;
c*d = a*b;
Output:
// ignore this line
a->b = 5;
c->d = a->b;
🔣Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.*; public class PointerConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<String> lines = new ArrayList<>(); while (sc.hasNextLine()) { String line = sc.nextLine(); if (line.trim().startsWith("//")) { lines.add(line); } else { lines.add(line.replaceAll("(\\w+)\\*(\\w+)", "$1->$2")); } } for (String line : lines) System.out.println(line); } } |
1 2 3 4 5 6 7 8 9 10 |
import re import sys lines = sys.stdin.read().splitlines() for line in lines: if line.strip().startswith("//"): print(line) else: print(re.sub(r'(\w+)\*(\w+)', r'\1->\2', line)) |
🔍 Explanation
Using regex, we safely convert pointer-like expressions to the -> format
without affecting comment lines that start with //.
✅ Problem 3: Previous 5 Characters
📝 Problem Statement
Given a capital letter C, print the 5 letters that precede it alphabetically.
🔢 Input
- A single uppercase letter C (C > ‘E’)
🧾 Output
- 5 space-separated characters before C
✅ Example
Input:
K
Output:
F G H I J
🔣Code
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Scanner; public class PrevFive { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char ch = sc.next().charAt(0); for (int i = 5; i >= 1; i--) { System.out.print((char)(ch - i) + " "); } } } |
1 2 3 4 |
ch = input().strip() for i in range(5, 0, -1): print(chr(ord(ch) - i), end=' ') |
🔍 Explanation
We use ASCII math to get previous characters using ord() and chr(),
then loop from -5 to -1 to print the correct sequence.
✅ Problem 4: Average of Middle Digits
📝 Problem Statement
Given an integer:
- If it has even digits → take 2 middle digits
- If odd → take 3 middle digits
Return the floor average of these digits.
🔢 Input
- A single integer with at least 2 digits
🧾 Output
- Integer — floor of the average
✅ Example
Input:
123456
Output:
4
(Middle digits: 3 and 4 → average = (3+4)//2 = 3)
🔣Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; public class MiddleDigitAverage { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String num = sc.next(); int len = num.length(); int sum; if (len % 2 == 0) { int mid1 = num.charAt(len/2 - 1) - '0'; int mid2 = num.charAt(len/2) - '0'; sum = (mid1 + mid2) / 2; } else { int mid1 = num.charAt(len/2 - 1) - '0'; int mid2 = num.charAt(len/2) - '0'; int mid3 = num.charAt(len/2 + 1) - '0'; sum = (mid1 + mid2 + mid3) / 3; } System.out.println(sum); } } |
1 2 3 4 5 6 7 8 9 10 11 |
num = input().strip() length = len(num) if length % 2 == 0: mid = length // 2 digits = [int(num[mid-1]), int(num[mid])] else: mid = length // 2 digits = [int(num[mid-1]), int(num[mid]), int(num[mid+1])] print(sum(digits) // len(digits)) |
🔍 Explanation
We get the middle digits based on string length and
calculate the integer (floor) average using integer division.
✅ Problem 5: First 4-Letter Word Position
📝 Problem Statement
Given a sentence, find the 1-based position of the first word with exactly 4 characters.
🔢 Input
- A sentence
🧾 Output
- Index of the first 4-letter word or -1
✅ Example
Input:
We love open code!
Output:
2
🔣Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.*; public class First4LetterWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] words = sc.nextLine().split("\\s+"); for (int i = 0; i < words.length; i++) { if (words[i].length() == 4) { System.out.println(i + 1); return; } } System.out.println(-1); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
words = input().split() idx = 1 found = False for word in words: if len(word) == 4: print(idx) found = True break idx += 1 if not found: print(-1) |
🔍 Explanation
The Java and Python programs take a line of text, break it into words, and find the first word that has exactly 4 letters. They then print its position (starting from 1), or -1 if no such word exists.
✅ Problem 6: Third Smallest Element Position
📝 Problem Statement
Given a list of integers, find the 1-based position of the third smallest unique element.
If there are fewer than 3 unique elements, return -1.
🔢 Input
- First line: integer n
- Second line: n space-separated integers
🧾 Output
- Index of third smallest unique number or -1
✅ Example
Input:
6
4 1 2 1 2 3
Output:
6
🔣Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.*; public class ThirdSmallestPosition { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(sc.nextInt()); Set<Integer> set = new TreeSet<>(list); if (set.size() < 3) { System.out.println(-1); } else { int count = 0, third = 0; for (int num : set) { count++; if (count == 3) { third = num; break; } } for (int i = 0; i < list.size(); i++) { if (list.get(i) == third) { System.out.println(i + 1); break; } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
n = int(input()) arr = list(map(int, input().split())) unique = sorted(set(arr)) if len(unique) < 3: print(-1) else: third = unique[2] for i, val in enumerate(arr): if val == third: print(i + 1) break |
🔍 Explanation
We create a sorted set to get unique values.
The third element is selected, and we find its first occurrence index (1-based).
🚨 Revature Overview!
Curious about how Revature’s selection process works, the rounds involved, and other key details? Don’t miss this complete breakdown:
🌐 https://nextgenkodinghub.in/revature-overview/
Wishing you the best of luck! 💼✨