Autocoder

Cadence Coding Round Questions with Solutions

Last updated 2026-11-19

Online tests and technical rounds for R&D software roles at a design-tools company are commonly reported to start with short C or C++ output questions and then move to trees, strings and linked lists. This page gives twelve practice problems in that order, each with a complete runnable solution, its complexity and the usual mistake.

They are practice problems in the style of topics such rounds are commonly reported to cover, not real papers. For the concept side, see Cadence interview questions, and for pointer-heavy drill see C++ pointers interview questions.

How to approach them

For output questions, find the single detail that changes the answer: a stray semicolon, a promoted type, an evaluation order. For tree and string problems, draw a three-node example first, state the base case, then write the code.

Mention recursion depth for trees. A tool that processes a huge hierarchy cannot assume the tree is shallow, so an iterative version is a plus.

Hiring status

No sourced hiring facts for Cadence are held on this site, so no dates, openings or packages are quoted. Use the company's own careers site.

Cadence Coding Round Questions with Solutions: questions with worked answers

  1. 1

    Predict the output: a stray semicolon after a for loop.

    easy

    The semicolon makes the loop body empty, so the loop runs three times doing nothing and i ends at 3; the following printf then runs once and prints 3. Output: 3. Common mistake: expecting 0 1 2. Compilers warn with -Wall about an empty body in a for statement, which is one reason to enable warnings.

    c
    #include <stdio.h>
    int main(void) {
        int i;
        for (i = 0; i < 3; i++);
        printf("%d\n", i);
        return 0;
    }
  2. 2

    Predict the output: integer promotion in a division, and a ternary with mixed types.

    medium

    5 / 2 is integer division and gives 2, which is converted to 2.0 when stored in a double. In 5 / 2.0 the integer is converted first, so the result is 2.5. The ternary below has an int and a double branch, so the result type is double, and 1 prints as 1.0. Output: 2.0 2.5 1.0. Common mistake: expecting 2.5 from 5 / 2 because the destination is a double.

    c
    #include <stdio.h>
    int main(void) {
        double a = 5 / 2;
        double b = 5 / 2.0;
        double c = 1 ? 1 : 2.5;
        printf("%.1f %.1f %.1f\n", a, b, c);
        return 0;
    }
  3. 3

    Predict the output: a character array modified through a pointer in a function.

    medium

    The function receives the address of the array, so changes persist in the caller: it converts each lowercase letter to uppercase by subtracting 32. The string "cadence" becomes "CADENCE". A string literal pointer could not be modified like this. Time O(n). Common mistake: passing a pointer to a string literal and writing to it, which is undefined.

    c
    #include <stdio.h>
    void up(char *s) {
        for (; *s; s++)
            if (*s >= 'a' && *s <= 'z') *s -= 32;
    }
    int main(void) {
        char s[] = "cadence";
        up(s);
        puts(s);  /* CADENCE */
        return 0;
    }
  4. 4

    Level-order traversal of a binary tree.

    medium

    Use a queue: process the nodes of one level at a time, recording the values and enqueuing the children. For the tree with root 3, children 9 and 20, and 20's children 15 and 7, the levels are [[3], [9, 20], [15, 7]]. Time O(n), space O(w) for the widest level w. Common mistake: not capturing the queue length at the start of each level, which merges levels.

    python
    from collections import deque
    
    class Node:
        def __init__(self, v, l=None, r=None):
            self.v, self.l, self.r = v, l, r
    
    def levels(root):
        out, q = [], deque([root] if root else [])
        while q:
            row = []
            for _ in range(len(q)):
                n = q.popleft()
                row.append(n.v)
                if n.l: q.append(n.l)
                if n.r: q.append(n.r)
            out.append(row)
        return out
    
    print(levels(Node(3, Node(9), Node(20, Node(15), Node(7)))))  # [[3], [9, 20], [15, 7]]
  5. 5

    Validate whether a binary tree is a binary search tree.

    medium

    Pass down the allowed open interval (low, high); every node must lie strictly inside it, and the children narrow it. Comparing only with the immediate children misses violations deeper in the tree, such as a node in the left subtree that is greater than the root. Time O(n), space O(h). Common mistake: the local-comparison check, which accepts the second tree below.

    python
    class Node:
        def __init__(self, v, l=None, r=None):
            self.v, self.l, self.r = v, l, r
    
    def is_bst(n, lo=float('-inf'), hi=float('inf')):
        if n is None:
            return True
        if not (lo < n.v < hi):
            return False
        return is_bst(n.l, lo, n.v) and is_bst(n.r, n.v, hi)
    
    good = Node(5, Node(3), Node(8))
    bad  = Node(5, Node(3, None, Node(7)), Node(8))   # 7 is in the left subtree of 5
    print(is_bst(good), is_bst(bad))  # True False
  6. 6

    Lowest common ancestor in a binary search tree.

    medium

    Starting at the root, if both values are smaller go left, if both are larger go right, otherwise the current node splits them and is the answer. For the BST with root 6, children 2 and 8, and 2's children 0 and 4, the LCA of 0 and 4 is 2 and of 2 and 8 is 6. Time O(h), space O(1) iteratively. Common mistake: using the general binary-tree algorithm and doing O(n) work.

    python
    class Node:
        def __init__(self, v, l=None, r=None):
            self.v, self.l, self.r = v, l, r
    
    def lca(root, a, b):
        while root:
            if a < root.v and b < root.v:
                root = root.l
            elif a > root.v and b > root.v:
                root = root.r
            else:
                return root.v
    
    t = Node(6, Node(2, Node(0), Node(4)), Node(8))
    print(lca(t, 0, 4), lca(t, 2, 8))  # 2 6
  7. 7

    Group a list of words into anagram classes.

    medium

    Use the sorted letters of each word as a dictionary key. For ["eat", "tea", "tan", "ate", "nat", "bat"] the groups are [eat, tea, ate], [tan, nat] and [bat]. Time O(n k log k) for n words of length k, space O(nk). A count-based key (26 counts) makes it O(nk). Common mistake: using a set of letters as the key, which merges "aab" and "abb".

    python
    from collections import defaultdict
    
    def group(words):
        d = defaultdict(list)
        for w in words:
            d["".join(sorted(w))].append(w)
        return list(d.values())
    
    print(group(["eat", "tea", "tan", "ate", "nat", "bat"]))
    # [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]
  8. 8

    Compress a string by run-length: "aaabbc" becomes "a3b2c1".

    medium

    Walk the string counting runs and append the character and its count. Decide whether to return the original if the compressed form is not shorter. "aaabbc" gives "a3b2c1", which is the same length, so the original would be returned under that rule. Time O(n), space O(n). Common mistake: dropping the final run because the loop only emits a run when the next character differs.

    python
    def compress(s):
        out, i = [], 0
        while i < len(s):
            j = i
            while j < len(s) and s[j] == s[i]:
                j += 1
            out.append(s[i] + str(j - i))
            i = j
        t = "".join(out)
        return t if len(t) < len(s) else s
    
    print(compress("aaabbc"), compress("aaaabbbbcc"))  # aaabbc a4b4c2
  9. 9

    Compute the water trapped between bars (the trapping rain water problem).

    hard

    At each position, the water above it is min(maxLeft, maxRight) minus its height. Two pointers do it in one pass: keep the running maxima from each end and always advance the side with the smaller maximum, because that side is limited by its own maximum. For [0,1,0,2,1,0,1,3,2,1,2,1] the answer is 6. Time O(n), space O(1). Common mistake: using only the global maximum, which overstates the water.

    python
    def trap(h):
        l, r = 0, len(h) - 1
        lm = rm = water = 0
        while l < r:
            if h[l] < h[r]:
                lm = max(lm, h[l]); water += lm - h[l]; l += 1
            else:
                rm = max(rm, h[r]); water += rm - h[r]; r -= 1
        return water
    
    print(trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]))  # 6
  10. 10

    Reverse a linked list in groups of k.

    hard

    Reverse each block of k nodes in place and link it to the next reversed block; if fewer than k nodes remain, leave them as they are. For 1-2-3-4-5 with k = 2 the result is 2-1-4-3-5. Time O(n), space O(1) iteratively. Common mistake: reversing the leftover short block when the problem says to leave it, and losing the link between blocks.

    python
    class Node:
        def __init__(self, v, nxt=None):
            self.v, self.next = v, nxt
    
    def reverse_k(head, k):
        probe, cnt = head, 0
        while probe and cnt < k:
            probe = probe.next; cnt += 1
        if cnt < k:
            return head
        prev, cur = reverse_k(probe, k), head
        for _ in range(k):
            cur.next, prev, cur = prev, cur, cur.next
        return prev
    
    def to_list(h):
        out = []
        while h:
            out.append(h.v); h = h.next
        return out
    
    h = Node(1, Node(2, Node(3, Node(4, Node(5)))))
    print(to_list(reverse_k(h, 2)))  # [2, 1, 4, 3, 5]
  11. 11

    Count the set bits of every number from 0 to n using dynamic programming.

    medium

    The count for i equals the count for i shifted right by one plus its lowest bit: bits[i] = bits[i >> 1] + (i & 1). For n = 8 the array is [0, 1, 1, 2, 1, 2, 2, 3, 1]. Time O(n), space O(n). Common mistake: calling a popcount loop for each i, which is O(n log n) when one pass was possible.

    python
    def count_bits(n):
        bits = [0] * (n + 1)
        for i in range(1, n + 1):
            bits[i] = bits[i >> 1] + (i & 1)
        return bits
    
    print(count_bits(8))  # [0, 1, 1, 2, 1, 2, 2, 3, 1]
  12. 12

    Check whether a string is a palindrome ignoring case and non-alphanumeric characters.

    medium

    Use two pointers from the ends, skip characters that are not letters or digits, compare lowercase forms and stop when they cross. "A man, a plan, a canal: Panama" is a palindrome and "race a car" is not. Time O(n), space O(1). Common mistake: building a cleaned copy and reversing it, which works but uses O(n) extra space when O(1) was possible.

    python
    def is_pal(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
    
    print(is_pal("A man, a plan, a canal: Panama"), is_pal("race a car"))  # True False

Frequently asked questions

What difficulty are the coding problems in such rounds?

Commonly reported are easy to medium problems on trees, strings, arrays and linked lists, with C or C++ output questions beforehand. A few medium-hard problems can appear in technical rounds.

Should I write code in C++ or Python?

C++ if the role is a C++ R&D role and the test allows it; otherwise the language you can write fastest and most correctly. Mention the C++ container you would use even if you code in Python.

Are these real Cadence problems?

No. They are practice problems in the style of topics such rounds are commonly reported to cover. The company does not publish its papers.

How do I handle deep recursion on trees?

State the depth risk, offer an iterative version with an explicit stack, as in the Cadence interview questions page, and test with a skewed tree.

Related guides

More in Company Interview Questions

See all in this section