Autocoder

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.

Problems10, mostly easy
LanguagesPython and Java
PatternsArrays, strings, lists, trees, simple DP
Reported test2 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

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. 1

    Second Largest Element in an array (distinct values).

    easy

    Track 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.

    python
    def 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 second
    java
    class 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. 2

    Palindrome Number without converting to a string.

    easy

    Reverse 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).

    python
    def 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 // 10
    java
    class 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. 3

    Climbing Stairs: ways to climb n steps taking 1 or 2 at a time.

    easy

    The 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.

    python
    def climb(n):
        a, b = 1, 1
        for _ in range(n - 1): a, b = b, a + b
        return b
    java
    class 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. 4

    Valid Anagram.

    easy

    Two 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.

    python
    from collections import Counter
    def is_anagram(s, t):
        return len(s) == len(t) and Counter(s) == Counter(t)
    java
    class 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. 5

    Binary Search in a sorted array.

    easy

    Keep 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.

    python
    def 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 -1
    java
    class 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. 6

    Reverse a Linked List.

    easy

    Walk 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.

    python
    def reverse(head):
        prev = None
        while head:
            nxt = head.next
            head.next = prev
            prev, head = head, nxt
        return prev
    java
    class 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. 7

    Move Zeroes to the end while keeping the order of other elements, in place.

    easy

    Keep 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.

    python
    def 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] = 0
    java
    class 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. 8

    Pascal's Triangle: generate the first n rows.

    easy

    Each 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.

    python
    def 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 rows
    java
    import 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. 9

    Maximum Depth of a Binary Tree.

    easy

    The 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.

    python
    def max_depth(root):
        if not root: return 0
        return 1 + max(max_depth(root.left), max_depth(root.right))
    java
    class 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. 10

    Missing Number in an array containing 0..n with one number absent.

    easy

    The 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).

    python
    def missing_number(a):
        x = len(a)
        for i, v in enumerate(a): x ^= i ^ v
        return x
    java
    class 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.

Sources

Related guides

More in Company Interview Questions

See all in this section