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
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)
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); } }
π 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
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))
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); } }
π 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
ch = input().strip() for i in range(5, 0, -1): print(chr(ord(ch) - i), end=' ')
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) + " "); } } }
π 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
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))
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); } }
π 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
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)
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); } }
π 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
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
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; } } } } }
π 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! πΌβ¨