IT Codes ZoneRevature

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! πŸ’Όβœ¨


Thanks for visiting! Explore the categories below for more exciting and useful content.


Leave a Reply

Your email address will not be published. Required fields are marked *