# SAP Labs Coding Questions

## Coding Questions asked in SAP

SAP Coding Questions on this page will help you to know more about the coding section of SAP recruitment drive. The section carries a heavy weightage and is an elimination round.

Get to know everything about SAP recruitment and coding questions on this page. You’ll get detailed information about SAP Eligibility Criteria, Recruitment Process and Salary Breakdown.

SAP enables the world to function more efficiently and enhances people’s lives through a global network of partners, clients, workers, and thought leaders.

As the pioneer in enterprise application software, we improve business operations for organisations of all sizes and across all sectors by redefining ERP and building networks of intelligent firms that offer supply chain transparency, resilience, and sustainability.

## Recruitment Process at SAP

The recruitment process of SAP is mentioned here which can help you to understand the process opted by SAP to recruit candidates for the position of SAP Scholar.

The SAP Recruitment Process contains the mentioned rounds.

• MCQ and Coding
• Virtual Interview

SAPRelated Information
Batch2023
CourseB.Tech (CS / IT / ECE / EE)
Education
• Graduation Marks – 70% and above
• 10th and 12th Marks – 60% and above apply
• No backlogs
Rounds
• MCQ and Coding is of 1hr 15 minutes
• Number of Sections – 3
• All rounds will be elimination rounds
Cost to Company (CTC)6.52 LPA
Salary Breakdown4.2LPA Stipend & 2.32LPA Benefits

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## SAP Coding Questions

### Question 1 : Max Unique Exponent

Problem Statement :

You’re given a string where multiple characters are repeated consecutively. You’re supposed to reduce the size of this string using mathematical logic given as in the example below :

Input :
aabbbbeeeeffggg

Output:
a2b4e4f2g3

Input :
abbccccc

Output:
ab2c5

Run
```#include <bits/stdc++.h>

using namespace std;
int ans = 0, mx = 0;

void Func(int a) {
int c = 0, k = a;
for (int i = 2; i <= a; i++)
while (a % i == 0) {
a /= i;
c++;
}
if (c > mx) {
mx = c;
ans = k;
}
}

int main() {
int a, b;
cin >> a >> b;
for (int i = a; i <= b; i++)
Func(i);
cout << mx << endl << ans;
}```
Run
```import java.util.Scanner;

public class Main {
static int ans = 0;
static int mx = 0;

public static void Func(int a) {
int c = 0;
int k = a;
for (int i = 2; i <= a; i++) {
while (a % i == 0) {
a /= i;
c++;
}
}
if (c > mx) {
mx = c;
ans = k;
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
for (int i = a; i <= b; i++)
Func(i);
System.out.println(mx + "\n" + ans);
}
}```
Run
```mx = 0
ans = 0

def Func(a):
global mx
global ans
c = 0
k = a
i = 1
while i <= a:
i += 1
while a % i == 0:
a /= i
c += 1
if c > mx:
mx = c
ans = k

a, b = map(int, input().split())
for i in range(a, b + 1):
Func(i)
print(mx)
print(ans)
```

### Question 2 : Sum of K Farthest items

Problem Statement  :

You are given an array of length “len” ,another item called k and an integer value x. Your job is to find the sum of k farthest items in the array from x.

First line has len, k and x respectively
2nd line has the array

Example :

Input :
5 3 20
21 4 15 17 11

Output :
30

4, 15 and 11 are farthest from 20. Thus, their sum will be the answer.

Run
```#include <bits/stdc++.h>

using namespace std;
int main() {
int len;
cin >> len;
int k;
cin >> k;
int x;
cin >> x;
vector < int > arr;
for (int i = 0; i < len; i++) {
int t;
cin >> t;
arr.push_back(t);

}
int max = INT_MIN;
int sum = 0, index = -1;
for (int i = 0; i < k; i++) {
max = INT_MIN;
for (int j = 0; j < len; j++) {
if (arr[j] == INT_MIN)
continue;
int temp = abs(x - arr[j]);
if (max < temp) {
max = temp;
index = j;
}
}
//System.out.println(arr[index]);
sum = sum + arr[index];
arr[index] = INT_MIN;
}
cout << sum;

return 0;
}```
Run
```import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
int k = sc.nextInt();
int x = sc.nextInt();
int arr[] = new int[len];
for (int i = 0; i < len; i++)
arr[i] = sc.nextInt();
int max = Integer.MIN_VALUE;
int sum = 0, index = -1;
for (int i = 0; i < k; i++) {
max = Integer.MIN_VALUE;
for (int j = 0; j < len; j++) {
if (arr[j] == Integer.MIN_VALUE)
continue;
int temp = (int) Math.abs(x - arr[j]);
if (max < temp) {
max = temp;
index = j;
}
}
sum = sum + arr[index];
arr[index] = Integer.MIN_VALUE;
}
System.out.println(sum);
}
}```
Run
```len, k, x = map(int, input().split())
arr = list(map(int, input().split()))
sum, index = 0, -1
for i in range(k):
max = -9999999
for j in range(len):
if arr[j] == -9999999:
continue
temp = abs(x - arr[j])
if max < temp:
max = temp
index = j
sum = sum + arr[index]
arr[index] = -9999999
print(sum)
```

### Question 3 : Find Nth largest and smallest

Problem Statement  :

Write a program to find the nth largest and nth smallest item in the array and print them in the same line.

Input Format
The first line has the following –
Size of array, value of n
2nd line has the array

Output Format
Nth largest and nth smallest respectively

Input :                                 Output :
6 3                                       3 4
1 2 3 4 5 6

Run
```#include <bits/stdc++.h>

using namespace std;

int main() {
int size, n;
cin >> size >> n;
vector < int > a(size);
for (int i = 0; i < size; i++) cin >> a[i];
sort(a.begin(), a.end());
cout << a[n - 1] << " " << a[size - n];
}```
Run
```import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int arr[] = new int[m];
for (int i = 0; i < m; i++)
arr[i] = sc.nextInt();
Arrays.sort(arr);
System.out.println(arr[n - 1] + " " + arr[m - n]);
}
}```
Run
```size, n = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
print(arr[n - 1], arr[-n])
```

### Question 4 : Prime Number Sum

Problem Statement  :

Write a function to solve the following equation :

a3 + a2b + 2a2b + 2ab2 + ab2 + b3.

Write a program to accept three values in order of a, b and c and get the result of the above equation.

Run
```//By solving above equation you will get it as (a+b)^3
//C is not used in the equation so we can omit that
#include <iostream>

using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
int sum;
sum = (a + b) * (a + b) * (a + b);
cout << sum;
}```
Run
```import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int sum;
sum = (a + b) * (a + b) * (a + b);
System.out.println(sum);
}
}```
Run
```# By solving above equation you will get it as (a+b)^3
# C is not used in the equation so we can omit that
a, b, c = map(int, input().split())
print((a + b) ** 3)
```

### Question 5 :Find no. of tyres

Problem Statement  :

In the city, there are a bunch of dealerships who sell bikes & cars. A function is there which tells how many dealerships there are and the total number of cars in each dealership.

Your job is to calculate how many tyres would be there in each dealership.
If you are using the predefined function use this structure,

Struct dealership {
Int cars;
Int bikes;
}

Input :                 Output :
3                          20
4 2                      16
4 0                       8
1 2

Explanation :

1. There are total 3 dealerships
2. Dealerships1 contains 4 cars and 2 bikes
3. Dealerships2 contains 4 cars and 0 bikes
4. Dealerships3 contains 1 cars and 2 bikes
5. Total number of tyres in dealerships1 is (4 x 4) + (2 x 2) = 20
6. Total number of tyres in dealerships2 is (4 x 4) + (0 x 2) = 16
7. Total number of tyres in dealerships3 is (1 x 4) + (2 x 2) = 8
Run
```#include <iostream>

using namespace std;
int main() {
int t, cars, bikes;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> cars >> bikes;
cout << cars * 4 + bikes * 2 << endl;
}
}```
Run
```import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dealership = sc.nextInt();
while (dealership--> 0) {
int cars = sc.nextInt();
int bikes = sc.nextInt();
System.out.println(cars * 4 + bikes * 2);
}
}
}```
Run
```for i in range(int(input())):
cars, bikes = map(int, input().split())
print(cars * 4 + bikes * 2)
```

## FAQs on SAP Coding Questions with Solutions

##### Question 1: What is the salary offered by SAP?

SAP offers a salary of 6.25 Lakhs per annum.

##### Question 2: Are all rounds in SAP elimination rounds?

Yes, all the rounds in SAP recruitment process are elimination rounds.

## 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