Autocoder

Birlasoft Coding Round Questions with Solutions

Last updated 2026-11-19

These ten easy-to-medium problems practise number properties, string validation and linked-list or frequency work, with Python and Java solutions, complexity and common mistakes. Birlasoft has not published its test content, so this is practice rather than a claimed paper.

Use it with Birlasoft interview questions and how to crack the DSA coding round.

LevelEasy to medium
LanguagesPython and Java shown
TopicsValidation, ranges, matrices, lists, counting

What these Birlasoft-style problems cover

Birlasoft does not publish its test content, so we make no claim about a particular paper. These ten problems are practice at the easy-to-medium level that IT services tests with a coding section commonly use: number properties, string validation, run detection, matrix checks, linked-list arithmetic and frequency counting. They are written to show clean handling of validation rules and edge cases, which is what such screening usually rewards.

How to practise validation-style problems

Birlasoft Coding Round Questions with Solutions: questions with worked answers

  1. 1

    Harshad (Niven) number check

    easy

    A number is Harshad if it is divisible by the sum of its digits (18 is divisible by 1 + 8 = 9). Sum the digits and test divisibility. Complexity: O(d) time, O(1) space. Common mistakes: dividing by a zero digit sum (only possible for 0) and treating negative numbers as valid.

    python
    def is_harshad(n):
        if n <= 0: return False
        return n % sum(int(c) for c in str(n)) == 0
    print(is_harshad(18), is_harshad(19))  # True False
    java
    class Solution {
        static boolean isHarshad(int n) {
            if (n <= 0) return false;
            int s = 0;
            for (int t = n; t > 0; t /= 10) s += t % 10;
            return n % s == 0;
        }
    }
  2. 2

    Validate an IPv4 address string

    medium

    Split on dots; require exactly four parts, each only digits, without leading zeros (except the single digit 0), and a value from 0 to 255. Complexity: O(n) time, O(1) space. Common mistakes: accepting empty parts, leading zeros such as 01, or non-digit characters that int() might still parse (like a plus sign).

    python
    def valid_ip(s):
        p = s.split('.')
        if len(p) != 4: return False
        for x in p:
            if not x.isdigit() or (len(x) > 1 and x[0] == '0') or int(x) > 255: return False
        return True
    print(valid_ip("192.168.1.1"), valid_ip("256.1.1.1"), valid_ip("1.01.1.1"))  # True False False
    java
    class Solution {
        static boolean validIp(String s) {
            String[] p = s.split("\\.", -1);
            if (p.length != 4) return false;
            for (String x : p) {
                if (x.isEmpty() || x.length() > 3) return false;
                for (char c : x.toCharArray()) if (c < '0' || c > '9') return false;
                if (x.length() > 1 && x.charAt(0) == '0') return false;
                if (Integer.parseInt(x) > 255) return false;
            }
            return true;
        }
    }
  3. 3

    Summary ranges of a sorted array

    medium

    Walk through the array grouping consecutive values. When the next value is not previous plus one, close the current range and format it as 'a->b', or just 'a' for a single number. Complexity: O(n) time, O(1) extra space. Common mistakes: overflow when computing previous plus one with large ints and forgetting to flush the final range after the loop.

    python
    def summary_ranges(a):
        out, i = [], 0
        while i < len(a):
            j = i
            while j + 1 < len(a) and a[j + 1] == a[j] + 1: j += 1
            out.append(str(a[i]) if i == j else f"{a[i]}->{a[j]}")
            i = j + 1
        return out
    print(summary_ranges([0, 1, 2, 4, 5, 7]))  # ['0->2', '4->5', '7']
    java
    import java.util.*;
    class Solution {
        static List<String> summaryRanges(int[] a) {
            List<String> out = new ArrayList<>();
            int i = 0;
            while (i < a.length) {
                int j = i;
                while (j + 1 < a.length && (long) a[j + 1] == (long) a[j] + 1) j++;
                out.add(i == j ? String.valueOf(a[i]) : a[i] + "->" + a[j]);
                i = j + 1;
            }
            return out;
        }
    }
  4. 4

    Intersection of two arrays keeping duplicates

    easy

    Count one array's elements in a map. For each element of the other, if its count is positive, add it to the result and decrement the count. Each shared occurrence is matched once. Complexity: O(m + n) time, O(min(m, n)) space. Common mistakes: using a set (drops duplicates) and not decrementing counts, which over-reports.

    python
    from collections import Counter
    def intersect(a, b):
        c, out = Counter(a), []
        for x in b:
            if c[x] > 0:
                out.append(x); c[x] -= 1
        return out
    print(intersect([1, 2, 2, 1], [2, 2]))  # [2, 2]
    java
    import java.util.*;
    class Solution {
        static List<Integer> intersect(int[] a, int[] b) {
            Map<Integer, Integer> c = new HashMap<>();
            for (int x : a) c.merge(x, 1, Integer::sum);
            List<Integer> out = new ArrayList<>();
            for (int x : b)
                if (c.getOrDefault(x, 0) > 0) { out.add(x); c.merge(x, -1, Integer::sum); }
            return out;
        }
    }
  5. 5

    Largest sum of a strictly increasing contiguous run

    easy

    Walk once, keeping the current run's sum; if the current element is greater than the previous add it, otherwise start a new run at this element. Track the maximum. Complexity: O(n) time, O(1) space. Common mistakes: resetting to zero instead of to the current element, and using non-strict comparison when the run must be strictly increasing.

    python
    def max_inc_sum(a):
        cur = best = a[0]
        for i in range(1, len(a)):
            cur = cur + a[i] if a[i] > a[i - 1] else a[i]
            best = max(best, cur)
        return best
    print(max_inc_sum([10, 20, 30, 5, 10, 50]))  # 65
    java
    class Solution {
        static int maxIncSum(int[] a) {
            int cur = a[0], best = a[0];
            for (int i = 1; i < a.length; i++) {
                cur = a[i] > a[i - 1] ? cur + a[i] : a[i];
                best = Math.max(best, cur);
            }
            return best;
        }
    }
  6. 6

    Is a square matrix symmetric?

    easy

    A matrix is symmetric if m[i][j] == m[j][i] for all i and j. Only compare the upper triangle against the lower one. Complexity: O(n squared) time, O(1) space. Common mistakes: comparing every cell twice and assuming the matrix is square without checking.

    python
    def symmetric(m):
        n = len(m)
        return all(len(r) == n for r in m) and all(m[i][j] == m[j][i] for i in range(n) for j in range(i + 1, n))
    print(symmetric([[1, 2], [2, 1]]), symmetric([[1, 2], [3, 1]]))  # True False
    java
    class Solution {
        static boolean symmetric(int[][] m) {
            int n = m.length;
            for (int[] r : m) if (r.length != n) return false;
            for (int i = 0; i < n; i++)
                for (int j = i + 1; j < n; j++) if (m[i][j] != m[j][i]) return false;
            return true;
        }
    }
  7. 7

    Count Pythagorean triplets up to N

    medium

    Check pairs a < b and test whether a squared plus b squared is a perfect square c squared with c at most N. Use a set of squares or an integer square root for the check. Complexity: O(N squared) time, O(1) space. Common mistakes: using floating-point square roots without rounding checks and counting (3,4,5) and (4,3,5) as two triplets.

    python
    from math import isqrt
    def count_triplets(n):
        cnt = 0
        for a in range(1, n + 1):
            for b in range(a + 1, n + 1):
                c = isqrt(a * a + b * b)
                if c <= n and c * c == a * a + b * b: cnt += 1
        return cnt
    print(count_triplets(20))  # 6
    java
    class Solution {
        static int countTriplets(int n) {
            int cnt = 0;
            for (int a = 1; a <= n; a++)
                for (int b = a + 1; b <= n; b++) {
                    int s = a * a + b * b;
                    int c = (int) Math.sqrt(s);
                    while (c * c < s) c++;
                    while (c * c > s) c--;
                    if (c <= n && c * c == s) cnt++;
                }
            return cnt;
        }
    }
  8. 8

    Add two numbers stored as reversed digit linked lists

    medium

    Walk both lists together adding digits and a carry, creating a node for sum mod 10 and carrying sum divided by 10. Continue while either list has nodes or a carry remains. Complexity: O(max(m, n)) time, O(max(m, n)) space. Common mistakes: stopping when one list ends and dropping the final carry node.

    python
    class Node:
        def __init__(self, v, nxt=None): self.v, self.next = v, nxt
    def add_lists(a, b):
        d = t = Node(0); carry = 0
        while a or b or carry:
            s = carry + (a.v if a else 0) + (b.v if b else 0)
            t.next = Node(s % 10); t = t.next; carry = s // 10
            a = a.next if a else None
            b = b.next if b else None
        return d.next
    r = add_lists(Node(2, Node(4, Node(3))), Node(5, Node(6, Node(4))))
    out = []
    while r: out.append(r.v); r = r.next
    print(out)  # [7, 0, 8]
    java
    class Solution {
        static class Node { int v; Node next; Node(int v) { this.v = v; } }
        static Node addLists(Node a, Node b) {
            Node d = new Node(0), t = d;
            int carry = 0;
            while (a != null || b != null || carry > 0) {
                int s = carry + (a != null ? a.v : 0) + (b != null ? b.v : 0);
                t.next = new Node(s % 10); t = t.next; carry = s / 10;
                if (a != null) a = a.next;
                if (b != null) b = b.next;
            }
            return d.next;
        }
    }
  9. 9

    Length of the longest palindrome that can be built from given letters

    easy

    Count characters. Every pair of equal letters can sit on both sides; add one extra character in the middle if any letter has an odd count. Complexity: O(n) time, O(k) space. Common mistakes: adding odd counts fully (which breaks symmetry) and forgetting the single middle character.

    python
    from collections import Counter
    def longest_pal_len(s):
        c = Counter(s)
        length = sum(v // 2 * 2 for v in c.values())
        return length + (1 if any(v % 2 for v in c.values()) else 0)
    print(longest_pal_len("abccccdd"))  # 7
    java
    class Solution {
        static int longestPalLen(String s) {
            int[] c = new int[128];
            for (char ch : s.toCharArray()) c[ch]++;
            int len = 0; boolean odd = false;
            for (int v : c) { len += v / 2 * 2; if (v % 2 == 1) odd = true; }
            return len + (odd ? 1 : 0);
        }
    }
  10. 10

    Find the second most frequent word

    medium

    Count words with a map, sort the distinct words by count descending, and pick the second. State the tie-break rule (first appearance or alphabetical) and what to return when fewer than two distinct words exist. Complexity: O(n + k log k) time, O(k) space. Common mistakes: breaking ties arbitrarily without saying so and forgetting case or punctuation normalisation.

    python
    from collections import Counter
    def second_frequent(words):
        c = Counter(w.lower() for w in words)
        ranked = sorted(c.items(), key=lambda p: (-p[1], p[0]))
        return ranked[1][0] if len(ranked) > 1 else None
    print(second_frequent("a b b c c c d d".split()))  # b (ties with d, alphabetical)
    java
    import java.util.*;
    class Solution {
        static String secondFrequent(String[] words) {
            Map<String, Integer> c = new HashMap<>();
            for (String w : words) c.merge(w.toLowerCase(), 1, Integer::sum);
            List<Map.Entry<String, Integer>> r = new ArrayList<>(c.entrySet());
            r.sort((x, y) -> !x.getValue().equals(y.getValue()) ? y.getValue() - x.getValue() : x.getKey().compareTo(y.getKey()));
            return r.size() > 1 ? r.get(1).getKey() : null;
        }
    }

Frequently asked questions

How hard is the Birlasoft coding test?

Not published. Prep material for IT services firms suggests easy to medium.

Are SQL questions part of the test?

Often in interviews for database-heavy roles; check your invitation for the test format.

Which language should I use?

Python or Java are both fine; use the one you debug fastest.

Where are Birlasoft notices?

On Birlasoft's own careers site.

Related guides

More in Company Interview Questions

See all in this section