SAP Coding Round Questions with Solutions in Python and Java
Last updated 2026-11-19
SAP Labs India's online test is reported to include two or three coding problems alongside aptitude and technical multiple-choice questions, so the coding problems are quick, fundamental and edge-case-sensitive. These ten problems cover arrays, strings, linked lists, trees and a basic dynamic programming problem.
Aim to solve each in under 15 minutes, then compare with the Python and Java solutions.
| Problems | 10, mostly easy |
|---|---|
| Languages | Python and Java |
| Patterns | Arrays, strings, lists, trees, simple DP |
| Reported test | 2 to 3 coding problems in 90 to 120 minutes |
What the SAP coding section is reported to look like
A third-party prep guide (not SAP) reports two to three coding problems in a 90 to 120 minute test that also has aptitude and technical multiple-choice sections, with a choice of Java, C++ or Python. With that much else in the paper, the coding problems are commonly practised at easy to easy-medium level, where speed and correct edge-case handling decide the result. The ten problems below fit that profile and are practice problems, not real test content.
Pair them with SAP interview questions for OOP, DBMS and OS.
Mistakes in easy problems
- Overlooking empty input, a single element and negative numbers.
- Off-by-one errors in loops and binary search bounds.
- Overflow when computing midpoints or sums in Java.
- Sorting when a linear pass is expected.
- Not testing against the sample before submitting.
Hiring status
SAP Labs India has opened a second Bengaluru campus with capacity for about 15,000 employees, and a SAP executive said in 2025 that it had hired 1,500 to 2,000 people a year on average. Check jobs.sap.com for current roles; this site has no SAP hiring-news page.
SAP Coding Round Questions with Solutions in Python and Java: questions with worked answers
1 Second Largest Element in an array (distinct values).
easyTrack the largest and second largest in one pass: when a value exceeds the largest, the old largest becomes second; otherwise if it is between them, it becomes second. Time O(n), space O(1). Mistake: sorting for a one-pass problem, or not handling arrays with fewer than two distinct values.
pythondef second_largest(a): first = second = float('-inf') for x in a: if x > first: first, second = x, first elif first > x > second: second = x return None if second == float('-inf') else secondjavaclass Solution { long secondLargest(int[] a) { long first = Long.MIN_VALUE, second = Long.MIN_VALUE; for (int x : a) { if (x > first) { second = first; first = x; } else if (x < first && x > second) second = x; } return second; } }2 Palindrome Number without converting to a string.
easyReverse only the second half of the number: pop digits from the end and push onto a reversed value until the original is no longer greater than the reversed part. For even length they are equal; for odd length, drop the middle digit with reversed // 10. Negative numbers and numbers ending in 0 (except 0) are not palindromes. Time O(log n), space O(1).
pythondef is_palindrome(x): if x < 0 or (x % 10 == 0 and x != 0): return False rev = 0 while x > rev: rev = rev * 10 + x % 10 x //= 10 return x == rev or x == rev // 10javaclass Solution { boolean isPalindrome(int x) { if (x < 0 || (x % 10 == 0 && x != 0)) return false; int rev = 0; while (x > rev) { rev = rev * 10 + x % 10; x /= 10; } return x == rev || x == rev / 10; } }3 Climbing Stairs: ways to climb n steps taking 1 or 2 at a time.
easyThe ways to reach step n equal the ways to reach n - 1 plus n - 2, which is the Fibonacci recurrence. Keep two variables, so time is O(n) and space O(1). Mistake: plain recursion without memoisation, which is exponential. See dynamic programming interview questions.
pythondef climb(n): a, b = 1, 1 for _ in range(n - 1): a, b = b, a + b return bjavaclass Solution { int climb(int n) { int a = 1, b = 1; for (int i = 2; i <= n; i++) { int t = a + b; a = b; b = t; } return b; } }4 Valid Anagram.
easyTwo strings are anagrams if their letter counts match. Count characters of the first, subtract those of the second, and check all counts are zero, after checking the lengths are equal. Time O(n), space O(1) for a fixed alphabet. Mistake: comparing sorted strings in O(n log n) when asked for linear, or forgetting the length check.
pythonfrom collections import Counter def is_anagram(s, t): return len(s) == len(t) and Counter(s) == Counter(t)javaclass Solution { boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; int[] c = new int[26]; for (int i = 0; i < s.length(); i++) { c[s.charAt(i) - 'a']++; c[t.charAt(i) - 'a']--; } for (int v : c) if (v != 0) return false; return true; } }5 Binary Search in a sorted array.
easyKeep lo and hi, compare the middle element to the target and discard half each step. Time O(log n), space O(1). Compute the midpoint as lo + (hi - lo) / 2 to avoid overflow, and loop while lo <= hi. Mistake: an off-by-one such as hi = mid or lo = mid causing an infinite loop.
pythondef binary_search(a, t): lo, hi = 0, len(a) - 1 while lo <= hi: mid = lo + (hi - lo) // 2 if a[mid] == t: return mid if a[mid] < t: lo = mid + 1 else: hi = mid - 1 return -1javaclass Solution { int search(int[] a, int t) { int lo = 0, hi = a.length - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (a[mid] == t) return mid; if (a[mid] < t) lo = mid + 1; else hi = mid - 1; } return -1; } }6 Reverse a Linked List.
easyWalk the list keeping a previous pointer; for each node save next, point node.next at previous, and advance. Return previous as the new head. Time O(n), space O(1). Mistake: overwriting node.next before saving it, which loses the rest of the list.
pythondef reverse(head): prev = None while head: nxt = head.next head.next = prev prev, head = head, nxt return prevjavaclass ListNode { int val; ListNode next; } class Solution { ListNode reverse(ListNode head) { ListNode prev = null; while (head != null) { ListNode nxt = head.next; head.next = prev; prev = head; head = nxt; } return prev; } }7 Move Zeroes to the end while keeping the order of other elements, in place.
easyKeep a write index for the next non-zero position; copy each non-zero value there and advance, then fill the rest with zeros. Or swap non-zeros forward, which does one pass with fewer writes when there are few zeros. Time O(n), space O(1). Mistake: creating a new array when the problem requires in-place work.
pythondef move_zeroes(a): w = 0 for x in a: if x != 0: a[w] = x; w += 1 for i in range(w, len(a)): a[i] = 0javaclass Solution { void moveZeroes(int[] a) { int w = 0; for (int x : a) if (x != 0) a[w++] = x; while (w < a.length) a[w++] = 0; } }8 Pascal's Triangle: generate the first n rows.
easyEach row starts and ends with 1, and each inner value is the sum of the two values above it. Build row by row from the previous row. Time O(n squared), space O(n squared) for output. Mistake: indexing errors at the edges; handle the first row separately, and n equal to zero.
pythondef generate(n): rows = [] for i in range(n): row = [1] * (i + 1) for j in range(1, i): row[j] = rows[i-1][j-1] + rows[i-1][j] rows.append(row) return rowsjavaimport java.util.*; class Solution { List<List<Integer>> generate(int n) { List<List<Integer>> rows = new ArrayList<>(); for (int i = 0; i < n; i++) { List<Integer> row = new ArrayList<>(Collections.nCopies(i + 1, 1)); for (int j = 1; j < i; j++) row.set(j, rows.get(i - 1).get(j - 1) + rows.get(i - 1).get(j)); rows.add(row); } return rows; } }9 Maximum Depth of a Binary Tree.
easyThe depth of a node is one plus the larger depth of its two subtrees, with an empty tree having depth zero. Recurse, or do a BFS counting levels to avoid deep recursion. Time O(n), space O(h) for recursion. Mistake: counting edges instead of nodes, or returning 1 for an empty tree.
pythondef max_depth(root): if not root: return 0 return 1 + max(max_depth(root.left), max_depth(root.right))javaclass TreeNode { int val; TreeNode left, right; } class Solution { int maxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }10 Missing Number in an array containing 0..n with one number absent.
easyThe XOR of 0..n and all array values leaves the missing number, since pairs cancel; the arithmetic alternative is n(n + 1)/2 minus the array sum. Time O(n), space O(1). Mistake: sorting first, which is O(n log n), or overflow in the sum formula for very large n (XOR avoids it).
pythondef missing_number(a): x = len(a) for i, v in enumerate(a): x ^= i ^ v return xjavaclass Solution { int missingNumber(int[] a) { int x = a.length; for (int i = 0; i < a.length; i++) x ^= i ^ a[i]; return x; } }
Frequently asked questions
How hard are SAP coding questions?
A prep guide reports easy to medium problems alongside other sections. Accuracy and edge cases matter more than advanced algorithms.
Which language should I use?
Java, C++ and Python are reported as allowed. Use the one you code fastest in.
Are these real SAP questions?
No. They are practice problems in the style of the reported test.
What should I read next?
How to crack the SAP interview and how to crack the online assessment.
Sources
Related guides
- SAP Labs Interview Questions and Answers for Freshers and SDE Roles
- Oracle Coding Round Questions with Solutions in Python and Java
- Cisco Coding Round Questions with Solutions in Python and Java
- Array interview questions with worked solutions
- String interview questions with worked solutions
- Dynamic Programming Interview Questions
More in Company Interview Questions
- SAP Labs Interview Questions and Answers for Freshers and SDE Roles
- Sasken coding round questions
- Sasken interview questions and answers
- ServiceNow Coding Round Questions With Solutions
- ServiceNow Interview Questions and Answers
- Siemens Coding Round Questions with Solutions
- Siemens Interview Questions and Answers for India Software Roles
- Sopra Steria coding round questions
- Sopra Steria interview questions and answers
- Sprinklr Coding Round Questions With Solutions
- Sprinklr Interview Questions and Answers
- Swiggy Coding Round Questions with Solutions