Autocoder

Barclays coding round questions with solutions

Last updated 2026-11-19

Barclays graduate technology assessments are commonly reported to combine aptitude and reasoning questions with coding problems of easy to medium difficulty. Hackathon-style rounds, such as Hack-o-Hire, call for working code under a deadline. The problems below are practice in that style: strings, arrays, binary search and SQL, plus two aptitude-style MCQs.

They are not recalled questions. Each has the approach, complexity, common mistake and runnable Python and Java. For the interviews, see Barclays interview questions.

StyleEasy to medium strings, arrays, binary search, SQL, aptitude
Languages herePython, Java, SQL
Problems8 coding, 2 MCQ-style
LevelEasy to medium

How to approach this set

Correctness on every test case matters more than speed of typing. For each problem, write down three test inputs before coding: a normal case, an empty or single-element case, and a worst case for size. Most lost marks in easy problems come from the second kind.

Practise the aptitude questions without a calculator, since many can be done with a quick factor. For the wider approach, see how to crack the aptitude test and how to crack an online assessment.

Barclays coding round questions with solutions: questions with worked answers

  1. 1

    Return the index of the first non-repeating character in a string, or -1.

    easy

    Count characters in a first pass, then scan again and return the first index whose count is 1. Time O(n), space O(k) for k distinct characters (constant for lowercase letters). A single pass with an ordered map also works. The common mistake is checking s.count(c) inside a loop, which is O(n^2) and fails on large inputs.

    python
    from collections import Counter
    def first_unique(s):
        c = Counter(s)
        for i, ch in enumerate(s):
            if c[ch] == 1:
                return i
        return -1
    java
    class Solution {
        static int firstUnique(String s) {
            int[] c = new int[128];
            for (char ch : s.toCharArray()) c[ch]++;
            for (int i = 0; i < s.length(); i++) if (c[s.charAt(i)] == 1) return i;
            return -1;
        }
    }
  2. 2

    Compress a string by replacing runs with character and count, e.g. aaabcc becomes a3b1c2.

    easy

    Walk the string once, tracking the current character and run length, and append the character and count when the run ends. Time O(n), space O(n) for the output. Build with a list or StringBuilder, never repeated string concatenation. Mistakes: forgetting to flush the final run after the loop, and not stating how counts of 10 or more are handled (they simply print as digits here).

    python
    def compress(s):
        if not s:
            return ""
        out, run = [], 1
        for i in range(1, len(s) + 1):
            if i < len(s) and s[i] == s[i - 1]:
                run += 1
            else:
                out.append(s[i - 1] + str(run))
                run = 1
        return ''.join(out)
    java
    class Solution {
        static String compress(String s) {
            if (s.isEmpty()) return "";
            StringBuilder out = new StringBuilder();
            int run = 1;
            for (int i = 1; i <= s.length(); i++) {
                if (i < s.length() && s.charAt(i) == s.charAt(i - 1)) run++;
                else { out.append(s.charAt(i - 1)).append(run); run = 1; }
            }
            return out.toString();
        }
    }
  3. 3

    Maximum subarray sum (Kadane's algorithm).

    medium

    Keep the best sum of a subarray ending at the current index: either extend the previous run or start fresh, whichever is larger. Track the global maximum. Time O(n), space O(1). Initialise with the first element, not 0, so an all-negative array returns its largest element instead of 0. For a version that also returns the indices, record where the run restarts.

    python
    def max_subarray(a):
        cur = best = a[0]
        for x in a[1:]:
            cur = max(x, cur + x)
            best = max(best, cur)
        return best
    java
    class Solution {
        static int maxSubarray(int[] a) {
            int cur = a[0], best = a[0];
            for (int i = 1; i < a.length; i++) {
                cur = Math.max(a[i], cur + a[i]);
                best = Math.max(best, cur);
            }
            return best;
        }
    }
  4. 4

    Return the elements of a matrix in spiral order.

    medium

    Maintain four boundaries (top, bottom, left, right) and walk right along the top row, down the right column, left along the bottom row and up the left column, shrinking each boundary after use. Check that top <= bottom and left <= right before the third and fourth passes, otherwise a single row or column is read twice. Time O(rows x cols), space O(1) beyond the output.

    python
    def spiral(m):
        out = []
        t, b, l, r = 0, len(m) - 1, 0, len(m[0]) - 1
        while t <= b and l <= r:
            out += m[t][l:r + 1]; t += 1
            for i in range(t, b + 1): out.append(m[i][r])
            r -= 1
            if t <= b:
                out += m[b][l:r + 1][::-1]; b -= 1
            if l <= r:
                for i in range(b, t - 1, -1): out.append(m[i][l])
                l += 1
        return out
    java
    import java.util.*;
    class Solution {
        static List<Integer> spiral(int[][] m) {
            List<Integer> out = new ArrayList<>();
            int t = 0, b = m.length - 1, l = 0, r = m[0].length - 1;
            while (t <= b && l <= r) {
                for (int j = l; j <= r; j++) out.add(m[t][j]);
                t++;
                for (int i = t; i <= b; i++) out.add(m[i][r]);
                r--;
                if (t <= b) { for (int j = r; j >= l; j--) out.add(m[b][j]); b--; }
                if (l <= r) { for (int i = b; i >= t; i--) out.add(m[i][l]); l++; }
            }
            return out;
        }
    }
  5. 5

    Capacity to ship packages within D days: packages must be shipped in order, and a day's load cannot exceed the ship's capacity. Find the minimum capacity.

    medium

    Binary search on the answer. The capacity lies between the heaviest package and the sum of all packages. For a candidate capacity, simulate loading greedily and count days; if days <= D it is feasible, so try smaller, otherwise larger. Time O(n log(sum)), space O(1). The lower bound must be the maximum weight, or a package may never fit. This is the standard template for 'minimise the maximum' problems; see binary search interview questions.

    python
    def ship(w, days):
        lo, hi = max(w), sum(w)
        while lo < hi:
            cap = (lo + hi) // 2
            d, load = 1, 0
            for x in w:
                if load + x > cap:
                    d += 1; load = 0
                load += x
            if d <= days: hi = cap
            else: lo = cap + 1
        return lo
    java
    class Solution {
        static int ship(int[] w, int days) {
            int lo = 0, hi = 0;
            for (int x : w) { lo = Math.max(lo, x); hi += x; }
            while (lo < hi) {
                int cap = (lo + hi) / 2, d = 1, load = 0;
                for (int x : w) {
                    if (load + x > cap) { d++; load = 0; }
                    load += x;
                }
                if (d <= days) hi = cap; else lo = cap + 1;
            }
            return lo;
        }
    }
  6. 6

    Merge two sorted arrays into the first, which has trailing space for the second.

    easy

    Fill from the back. Compare the largest remaining elements of both arrays and write the bigger one at the last free position, moving the pointers left. When the second array is exhausted the rest is already in place. Time O(m + n), space O(1). Filling from the front would overwrite values not yet compared, which is the common bug.

    python
    def merge(a, m, b, n):
        i, j, k = m - 1, n - 1, m + n - 1
        while j >= 0:
            if i >= 0 and a[i] > b[j]:
                a[k] = a[i]; i -= 1
            else:
                a[k] = b[j]; j -= 1
            k -= 1
    java
    class Solution {
        static void merge(int[] a, int m, int[] b, int n) {
            int i = m - 1, j = n - 1, k = m + n - 1;
            while (j >= 0) {
                if (i >= 0 && a[i] > b[j]) a[k--] = a[i--];
                else a[k--] = b[j--];
            }
        }
    }
  7. 7

    Does the array contain two equal values whose indices differ by at most k?

    easy

    Keep a set that holds the last k values as a sliding window. For each element, if it is already in the set return true; otherwise add it and, once the window exceeds k, remove the element that has left. Time O(n), space O(k). Using a map from value to last index is an equivalent variant. The mistake is leaving old elements in the set, which reports duplicates that are too far apart.

    python
    def near_duplicate(a, k):
        win = set()
        for i, x in enumerate(a):
            if x in win:
                return True
            win.add(x)
            if len(win) > k:
                win.remove(a[i - k])
        return False
    java
    import java.util.*;
    class Solution {
        static boolean nearDuplicate(int[] a, int k) {
            Set<Integer> win = new HashSet<>();
            for (int i = 0; i < a.length; i++) {
                if (!win.add(a[i])) return true;
                if (win.size() > k) win.remove(a[i - k]);
            }
            return false;
        }
    }
  8. 8

    SQL: list the departments whose average salary is above the company-wide average.

    medium

    Group by department and use HAVING to compare each group's average with a scalar subquery for the overall average. WHERE cannot filter on an aggregate, which is why HAVING is needed. Mention that AVG ignores NULL salaries, so decide whether NULLs should count as zero.

    sql
    SELECT dept_id, AVG(salary) AS avg_salary
    FROM Employee
    GROUP BY dept_id
    HAVING AVG(salary) > (SELECT AVG(salary) FROM Employee);
  9. 9

    Aptitude MCQ: a shopkeeper marks an item 20% above cost price and then gives a 10% discount. What is the profit percentage?

    easy

    Take cost price as 100. The marked price is 120, and after a 10% discount the selling price is 120 x 0.9 = 108, so the profit is 8%. The distractor is 10%, from subtracting 10 from 20. For successive percentage changes, multiply the factors: 1.20 x 0.90 = 1.08.

  10. 10

    Reasoning MCQ: find the next number in the series 2, 6, 12, 20, 30, ?

    easy

    The differences are 4, 6, 8, 10, so the next difference is 12 and the answer is 42. Equivalently the terms are n(n+1): 1x2, 2x3, 3x4, 4x5, 5x6, so the next is 6x7 = 42. Always compute first and second differences before looking for a formula.

Frequently asked questions

How hard is the Barclays coding round?

Reports describe easy to medium problems on strings, arrays and basic algorithms, with aptitude questions in some cycles. Hackathon rounds are more open-ended.

Does the test include SQL?

Some technology tracks are reported to include SQL questions. Prepare joins, grouping and HAVING.

Which language should I use?

Use the language you are fastest in. Java and Python are both widely accepted on assessment platforms.

Are these real Barclays 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