Autocoder

HSBC coding round questions with solutions

Last updated 2026-11-19

HSBC graduate technology assessments are commonly described as a mix of online tests, including coding problems at easy to medium difficulty and sometimes logic or technical MCQs. Stacks, two pointers, binary search and simple maths problems are good practice for that level. The ten problems below follow that style, with two language MCQs. None is a recalled question.

Each answer has the approach, complexity, the common mistake and runnable Python and Java. For interviews, see HSBC interview questions.

StyleEasy to medium stacks, pointers, binary search, SQL, language MCQs
Languages herePython, Java, SQL
Problems7 coding, 1 SQL, 2 MCQ
LevelEasy to medium

How to approach this set

At easy and medium difficulty, correctness and clean edge handling decide the score. For each problem, list two or three tricky inputs before coding: an empty input, an input made of all the same value and a maximum-size input.

For the MCQs, trace the code by hand rather than guessing; evaluation order and aliasing are the usual traps. See how to crack a technical MCQ test and how to crack an online assessment.

HSBC coding round questions with solutions: questions with worked answers

  1. 1

    Valid parentheses: check that every bracket in a string of (), {} and [] is closed in the right order.

    easy

    Push openers on a stack; for a closer, the stack must be non-empty and its top must be the matching opener. At the end the stack must be empty. Time O(n), space O(n). Early exit on odd length is a cheap optimisation. Mistake: counting brackets per type, which accepts '([)]'.

    python
    def is_valid(s):
        pairs = {')': '(', ']': '[', '}': '{'}
        st = []
        for ch in s:
            if ch in pairs:
                if not st or st.pop() != pairs[ch]:
                    return False
            else:
                st.append(ch)
        return not st
    java
    import java.util.*;
    class Solution {
        static boolean isValid(String s) {
            Deque<Character> st = new ArrayDeque<>();
            for (char ch : s.toCharArray()) {
                if (ch == '(' || ch == '[' || ch == '{') st.push(ch);
                else {
                    if (st.isEmpty()) return false;
                    char o = st.pop();
                    if ((ch == ')' && o != '(') || (ch == ']' && o != '[') || (ch == '}' && o != '{')) return false;
                }
            }
            return st.isEmpty();
        }
    }
  2. 2

    Sort an array of 0s, 1s and 2s in place in one pass (the Dutch national flag problem).

    medium

    Keep three regions with pointers low, mid and high. If a[mid] is 0, swap with low and advance both; if 1, advance mid; if 2, swap with high and decrease high without advancing mid, because the swapped-in value is unexamined. Time O(n), space O(1). Counting sort is two passes. The common bug is advancing mid after swapping with high.

    python
    def sort_colors(a):
        lo, mid, hi = 0, 0, len(a) - 1
        while mid <= hi:
            if a[mid] == 0:
                a[lo], a[mid] = a[mid], a[lo]
                lo += 1; mid += 1
            elif a[mid] == 1:
                mid += 1
            else:
                a[mid], a[hi] = a[hi], a[mid]
                hi -= 1
    java
    class Solution {
        static void sortColors(int[] a) {
            int lo = 0, mid = 0, hi = a.length - 1;
            while (mid <= hi) {
                if (a[mid] == 0) { int t = a[lo]; a[lo++] = a[mid]; a[mid++] = t; }
                else if (a[mid] == 1) mid++;
                else { int t = a[mid]; a[mid] = a[hi]; a[hi--] = t; }
            }
        }
    }
  3. 3

    Plus one: add one to a large number stored as an array of digits.

    easy

    Start from the last digit. If it is below 9, increment and return; otherwise set it to 0 and carry to the next digit. If every digit was 9, the result is 1 followed by zeros, so build a new array one longer. Time O(n), space O(1) except for the all-nines case. Mistake: converting to an int, which overflows for long digit arrays.

    python
    def plus_one(d):
        for i in range(len(d) - 1, -1, -1):
            if d[i] < 9:
                d[i] += 1
                return d
            d[i] = 0
        return [1] + d
    java
    class Solution {
        static int[] plusOne(int[] d) {
            for (int i = d.length - 1; i >= 0; i--) {
                if (d[i] < 9) { d[i]++; return d; }
                d[i] = 0;
            }
            int[] r = new int[d.length + 1];
            r[0] = 1;
            return r;
        }
    }
  4. 4

    Find a peak element: an index whose value is greater than its neighbours, in O(log n).

    medium

    Binary search on the slope. Compare a[mid] with a[mid + 1]: if the next element is larger, a peak lies to the right (the array rises, and the edge counts as negative infinity); otherwise a peak lies at mid or to the left. Time O(log n), space O(1). Any peak is acceptable, not necessarily the global maximum. Mistake: comparing with mid - 1 and mid + 1 without guarding the array edges.

    python
    def find_peak(a):
        lo, hi = 0, len(a) - 1
        while lo < hi:
            mid = (lo + hi) // 2
            if a[mid] < a[mid + 1]:
                lo = mid + 1
            else:
                hi = mid
        return lo
    java
    class Solution {
        static int findPeak(int[] a) {
            int lo = 0, hi = a.length - 1;
            while (lo < hi) {
                int mid = (lo + hi) / 2;
                if (a[mid] < a[mid + 1]) lo = mid + 1; else hi = mid;
            }
            return lo;
        }
    }
  5. 5

    Happy number: repeatedly replace a number by the sum of the squares of its digits. Does it reach 1?

    easy

    Either the process reaches 1, or it enters a cycle. Detect the cycle with a set of seen values or with Floyd's slow and fast pointers on the digit-square function. Time is small and bounded in practice, since the numbers shrink quickly (about O(log n) per step), and space is O(1) with Floyd. For example 19 gives 82, 68, 100, 1, so it is happy; 4 enters the cycle 4, 16, 37, 58, 89, 145, 42, 20, 4. Mistake: looping forever without cycle detection.

    python
    def is_happy(n):
        def step(x):
            s = 0
            while x:
                x, d = divmod(x, 10)
                s += d * d
            return s
        slow, fast = n, step(n)
        while fast != 1 and slow != fast:
            slow, fast = step(slow), step(step(fast))
        return fast == 1
    java
    class Solution {
        static int step(int x) {
            int s = 0;
            while (x > 0) { int d = x % 10; s += d * d; x /= 10; }
            return s;
        }
        static boolean isHappy(int n) {
            int slow = n, fast = step(n);
            while (fast != 1 && slow != fast) { slow = step(slow); fast = step(step(fast)); }
            return fast == 1;
        }
    }
  6. 6

    Excel column title: convert a column number such as 28 into letters (AB).

    easy

    This is bijective base 26 with digits 1 to 26 (A to Z) and no zero. Repeatedly subtract 1 from n, take n mod 26 as the next letter (A plus the remainder), prepend it, and divide n by 26. Time O(log26 n), space O(log n) for the result. Mistake: using ordinary base 26 conversion, which breaks at 26 (Z) and 52 (AZ); the subtract-one step fixes it.

    python
    def column_title(n):
        out = []
        while n:
            n -= 1
            out.append(chr(ord('A') + n % 26))
            n //= 26
        return ''.join(reversed(out))
    java
    class Solution {
        static String columnTitle(int n) {
            StringBuilder sb = new StringBuilder();
            while (n > 0) {
                n--;
                sb.append((char) ('A' + n % 26));
                n /= 26;
            }
            return sb.reverse().toString();
        }
    }
  7. 7

    Intersection of two arrays with duplicates: each value appears as many times as it does in both arrays.

    easy

    Count the smaller array in a hash map, then scan the other array and, for each value with a positive count, add it to the result and decrement the count. Time O(m + n), space O(min(m, n)). If both arrays are sorted, two pointers give O(1) extra space. Mistake: using sets, which loses duplicates.

    python
    from collections import Counter
    def intersect(a, b):
        if len(a) > len(b):
            a, b = b, a
        c, out = Counter(a), []
        for x in b:
            if c[x] > 0:
                out.append(x)
                c[x] -= 1
        return out
    java
    import java.util.*;
    class Solution {
        static List<Integer> intersect(int[] a, int[] b) {
            if (a.length > b.length) { int[] t = a; a = b; b = t; }
            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) {
                int k = c.getOrDefault(x, 0);
                if (k > 0) { out.add(x); c.put(x, k - 1); }
            }
            return out;
        }
    }
  8. 8

    SQL: find managers who have at least five direct reports.

    medium

    Group employees by manager_id, keep groups with a count of at least five using HAVING, and join back to the employee table for the manager's name. Employees with no manager have a NULL manager_id and form their own group, which the join to the manager's row removes. This is a typical self-referencing table exercise.

    sql
    SELECT m.name
    FROM Employee m
    JOIN (SELECT manager_id FROM Employee GROUP BY manager_id HAVING COUNT(*) >= 5) r
      ON m.id = r.manager_id;
  9. 9

    Java MCQ: what does System.out.println(1 + 2 + "3" + 4 + 5); print?

    easy

    It prints 3345. Evaluation is left to right: 1 + 2 is integer addition giving 3; then 3 + "3" is string concatenation giving "33"; then "33" + 4 gives "334" and "334" + 5 gives "3345". Once a String appears, every following + concatenates. Putting brackets around 4 + 5 would give 339.

  10. 10

    Python MCQ: a = [1, 2]; b = a; b.append(3); print(a). What is printed?

    easy

    [1, 2, 3]. Assignment b = a copies the reference, not the list, so both names point to the same object and the append is visible through a. To copy, use a.copy(), a[:] or list(a) for a shallow copy, and copy.deepcopy for nested structures. The same aliasing causes bugs when a list is passed to a function and modified there.

Frequently asked questions

How hard is the HSBC coding test?

It is commonly described as easy to medium. Practise fundamentals until they are quick and accurate.

Are there MCQs on languages?

Some graduate cycles are reported to include technical MCQs on Java, Python or CS basics. Trace code by hand when answering.

Does the process include video interviews?

A recorded video interview is commonly reported in some programmes. See the HSBC interview questions guide for how to prepare.

Are these the real questions?

No. They are original practice problems in the style of the topics the round commonly covers.

Related guides

More in Company Interview Questions

See all in this section