Autocoder

Fidelity Investments coding round questions with solutions

Last updated 2026-11-19

Fidelity Investments early-career assessments are commonly described as aptitude plus coding at easy to medium difficulty, with technical MCQs in some cycles. Linked lists, strings, trees, simple DP and heaps are good practice for that level, with some SQL. The ten problems below follow that style. None is a recalled question.

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

StyleEasy to medium lists, strings, trees, DP, heaps, SQL, MCQs
Languages herePython, Java, SQL
Problems7 coding, 1 SQL, 2 MCQ
LevelEasy to hard

How to approach this set

For easy and medium tests, accuracy on every case beats speed. After writing each solution, run it on the smallest input, an input that uses every branch and the largest allowed input, and fix anything that differs from your expectation.

The DP and heap problems each have a naive version that is easy to write and a better one that needs one insight; write the naive one first and state its complexity. See dynamic programming interview questions, how to crack an online assessment and DSA coding rounds.

Fidelity Investments coding round questions with solutions: questions with worked answers

  1. 1

    Add two numbers stored as linked lists of digits in reverse order (least significant digit first).

    medium

    Walk both lists together, adding the two digits and the carry; the new digit is the sum mod 10 and the carry is the sum divided by 10. Continue while either list has nodes or the carry is non-zero, so a final carry creates an extra node. Time O(max(m, n)), space O(1) beyond the output. Mistake: stopping when both lists end and dropping a remaining carry, as in 99 + 1.

    python
    class ListNode:
        def __init__(self, val=0, next=None):
            self.val, self.next = val, next
    
    def add_two(a, b):
        dummy = tail = ListNode()
        carry = 0
        while a or b or carry:
            s = carry + (a.val if a else 0) + (b.val if b else 0)
            carry, digit = divmod(s, 10)
            tail.next = ListNode(digit)
            tail = tail.next
            a = a.next if a else None
            b = b.next if b else None
        return dummy.next
    java
    class Solution {
        static class ListNode { int val; ListNode next; ListNode(int v) { val = v; } }
        static ListNode addTwo(ListNode a, ListNode b) {
            ListNode dummy = new ListNode(0), tail = dummy;
            int carry = 0;
            while (a != null || b != null || carry != 0) {
                int s = carry + (a != null ? a.val : 0) + (b != null ? b.val : 0);
                carry = s / 10;
                tail.next = new ListNode(s % 10);
                tail = tail.next;
                if (a != null) a = a.next;
                if (b != null) b = b.next;
            }
            return dummy.next;
        }
    }
  2. 2

    Longest common prefix of an array of strings.

    easy

    Take the first string as the candidate prefix and shorten it until each other string starts with it. Alternatively compare character by character down all strings (vertical scanning). Time O(S), the total number of characters, space O(1). Return an empty string for an empty array. Mistake: indexing past the end of a shorter string; stop at the shortest length.

    python
    def common_prefix(strs):
        if not strs:
            return ""
        prefix = strs[0]
        for s in strs[1:]:
            while not s.startswith(prefix):
                prefix = prefix[:-1]
        return prefix
    java
    class Solution {
        static String commonPrefix(String[] strs) {
            if (strs.length == 0) return "";
            String prefix = strs[0];
            for (int i = 1; i < strs.length; i++)
                while (!strs[i].startsWith(prefix)) prefix = prefix.substring(0, prefix.length() - 1);
            return prefix;
        }
    }
  3. 3

    Valid palindrome: ignoring case and non-alphanumeric characters, is the string a palindrome?

    easy

    Use two pointers from both ends, skipping characters that are not letters or digits, and compare the lowercase versions of the rest. Time O(n), space O(1). Building a cleaned copy and comparing with its reverse is simpler but uses O(n) space. Mistake: forgetting to skip non-alphanumeric characters on both sides, and treating an empty cleaned string as invalid (it is a palindrome).

    python
    def is_palindrome(s):
        i, j = 0, len(s) - 1
        while i < j:
            while i < j and not s[i].isalnum():
                i += 1
            while i < j and not s[j].isalnum():
                j -= 1
            if s[i].lower() != s[j].lower():
                return False
            i += 1; j -= 1
        return True
    java
    class Solution {
        static boolean isPalindrome(String s) {
            int i = 0, j = s.length() - 1;
            while (i < j) {
                while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++;
                while (i < j && !Character.isLetterOrDigit(s.charAt(j))) j--;
                if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) return false;
                i++; j--;
            }
            return true;
        }
    }
  4. 4

    In-order traversal of a binary tree without recursion.

    medium

    Use an explicit stack: go as far left as possible pushing nodes, pop one, visit it, then move to its right child and repeat. Time O(n), space O(h). It avoids stack overflow on very deep trees. For a BST the output is sorted. Mistake: visiting a node when it is pushed instead of when it is popped, which gives pre-order.

    python
    def inorder(root):
        out, st, cur = [], [], root
        while cur or st:
            while cur:
                st.append(cur)
                cur = cur.left
            cur = st.pop()
            out.append(cur.val)
            cur = cur.right
        return out
    java
    import java.util.*;
    class Solution {
        static class TreeNode { int val; TreeNode left, right; }
        static List<Integer> inorder(TreeNode root) {
            List<Integer> out = new ArrayList<>();
            Deque<TreeNode> st = new ArrayDeque<>();
            TreeNode cur = root;
            while (cur != null || !st.isEmpty()) {
                while (cur != null) { st.push(cur); cur = cur.left; }
                cur = st.pop();
                out.add(cur.val);
                cur = cur.right;
            }
            return out;
        }
    }
  5. 5

    House robber: maximise the sum of values taken from a row of houses without taking two adjacent ones.

    medium

    At each house either skip it (best so far) or rob it (best from two houses back plus its value). Keep the two previous bests: new = max(prev1, prev2 + x). Time O(n), space O(1). Greedy picking of the largest values fails, for example [2, 3, 2] where robbing both ends gives 4 instead of 3. See dynamic programming interview questions.

    python
    def rob(a):
        prev2 = prev1 = 0
        for x in a:
            prev2, prev1 = prev1, max(prev1, prev2 + x)
        return prev1
    java
    class Solution {
        static int rob(int[] a) {
            int prev2 = 0, prev1 = 0;
            for (int x : a) {
                int cur = Math.max(prev1, prev2 + x);
                prev2 = prev1;
                prev1 = cur;
            }
            return prev1;
        }
    }
  6. 6

    Unique paths: count the paths from the top-left to the bottom-right of an m x n grid moving only right or down.

    medium

    Each path has m - 1 down moves and n - 1 right moves in some order, so the count is C(m + n - 2, m - 1). Compute it iteratively with multiplication and exact division at each step to avoid large factorials. Time O(min(m, n)), space O(1). The DP alternative, where each cell is the sum of the cell above and to its left, is O(mn). Mistake: computing factorials directly, which overflows quickly.

    python
    def unique_paths(m, n):
        r = 1
        k = min(m - 1, n - 1)
        for i in range(1, k + 1):
            r = r * (m + n - 2 - k + i) // i
        return r
    java
    class Solution {
        static long uniquePaths(int m, int n) {
            long r = 1;
            int k = Math.min(m - 1, n - 1);
            for (int i = 1; i <= k; i++) r = r * (m + n - 2 - k + i) / i;
            return r;
        }
    }
  7. 7

    Sort a k-sorted array: every element is at most k positions from its sorted position.

    hard

    Use a min-heap of size k + 1. Push the first k + 1 elements, then repeatedly pop the smallest into the output and push the next element. The smallest of any k + 1 consecutive elements must be the next in sorted order. Time O(n log k), space O(k), better than O(n log n) for small k. Mistake: using a heap of size k, which is one short and can emit an element too early.

    python
    import heapq
    def sort_k_sorted(a, k):
        h = a[:k + 1]
        heapq.heapify(h)
        out = []
        for x in a[k + 1:]:
            out.append(heapq.heappushpop(h, x))
        while h:
            out.append(heapq.heappop(h))
        return out
    java
    import java.util.*;
    class Solution {
        static int[] sortKSorted(int[] a, int k) {
            PriorityQueue<Integer> h = new PriorityQueue<>();
            int[] out = new int[a.length];
            int idx = 0;
            for (int i = 0; i < a.length; i++) {
                h.add(a[i]);
                if (h.size() > k) out[idx++] = h.poll();
            }
            while (!h.isEmpty()) out[idx++] = h.poll();
            return out;
        }
    }
  8. 8

    SQL: find the customers with the most orders, including ties.

    medium

    Count orders per customer and compare with the maximum count. RANK or DENSE_RANK over the counts, ordered descending, and filtering for rank 1 keeps all tied customers. LIMIT 1 returns only one customer and silently drops ties, which is the common mistake. Decide whether customers with zero orders should be considered.

    sql
    SELECT customer_id, cnt
    FROM (
      SELECT customer_id, COUNT(*) AS cnt,
             RANK() OVER (ORDER BY COUNT(*) DESC) AS rnk
      FROM Orders
      GROUP BY customer_id
    ) t
    WHERE rnk = 1;
  9. 9

    SQL MCQ: what does SELECT COUNT(*) FROM t WHERE x = NULL return?

    easy

    It returns 0. In SQL, NULL means unknown, so any comparison with NULL using = or <> evaluates to unknown, not true, and the row is filtered out. Use x IS NULL (or IS NOT NULL) to test for NULL. COUNT(x) ignores NULLs while COUNT(*) counts every row, and NOT IN against a subquery containing NULL returns nothing, which is a classic trap.

  10. 10

    Java MCQ: String s = "a"; s.concat("b"); System.out.println(s); What is printed?

    easy

    It prints a. Strings are immutable, so concat returns a new String and does not change s; the result was discarded. The fix is s = s.concat("b"), which prints ab. The same applies to replace, toUpperCase, trim and substring, all of which return new strings. For many modifications, use StringBuilder.

Frequently asked questions

How hard is the Fidelity coding test?

It is commonly described as easy to medium. This set covers that range with a few harder problems.

Is there an aptitude section?

Some early-career cycles are reported to include aptitude questions. Check your invitation.

Does the test include SQL?

Some technology roles are reported to include SQL. Practise ranking with ties, grouping and NULL handling.

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