Autocoder

Oracle Coding Round Questions with Solutions in Python and Java

Last updated 2026-11-19

Oracle's coding rounds are described as knockout rounds on data structures such as arrays, strings, linked lists, trees and hash maps. These ten problems practise exactly those structures and the patterns behind them.

Attempt each one yourself, then compare with the worked Python and Java solutions, which give complexity and the mistakes to avoid.

Problems10, easy to hard
LanguagesPython and Java
PatternsLinked lists, trees, sliding window, in-place arrays
Rounds describedTwo coding rounds, knockout

What the two coding rounds look like

Oracle's recruiter-authored blog posts describe two coding rounds as knockout rounds focused on problem solving, on topics such as arrays, strings, linked lists, trees, hash maps and graphs, with attention to big-O analysis. The ten problems below cover those structures at medium difficulty and are practice problems, not real interview content. The posts are older, so confirm the current format with your recruiter.

For database and Java fundamentals that often accompany these rounds, see Oracle interview questions.

Mistakes that knock candidates out

Hiring status

For current Oracle roles, see Oracle hiring and oracle.com/in/careers. Oracle reportedly cut about 10,000 to 12,000 roles in India from 31 March 2026, which Oracle did not confirm, so verify openings on its careers site.

Oracle Coding Round Questions with Solutions in Python and Java: questions with worked answers

  1. 1

    Reverse Words in a String (collapse extra spaces).

    easy

    Split on whitespace so empty tokens vanish, reverse the token list and join with single spaces. Time O(n), space O(n). An in-place version for a char array reverses the whole array and then each word. Mistake: splitting on a single space, which leaves empty strings when there are multiple spaces.

    python
    def reverse_words(s):
        return ' '.join(reversed(s.split()))
    java
    import java.util.*;
    class Solution {
        String reverseWords(String s) {
            String[] w = s.trim().split("\\s+");
            Collections.reverse(Arrays.asList(w));
            return String.join(" ", w);
        }
    }
  2. 2

    Remove the Nth Node From the End of a linked list in one pass.

    medium

    Use two pointers with a dummy head: advance the first pointer n + 1 steps, then move both until the first reaches the end; the second is then just before the node to remove. Time O(n), space O(1). The dummy node handles removing the head. Mistake: advancing only n steps so the second pointer lands on the node itself.

    python
    def remove_nth(head, n):
        dummy = ListNode(0); dummy.next = head
        fast = slow = dummy
        for _ in range(n + 1): fast = fast.next
        while fast:
            fast, slow = fast.next, slow.next
        slow.next = slow.next.next
        return dummy.next
    java
    class ListNode { int val; ListNode next; ListNode(int v) { val = v; } }
    class Solution {
        ListNode removeNth(ListNode head, int n) {
            ListNode dummy = new ListNode(0); dummy.next = head;
            ListNode fast = dummy, slow = dummy;
            for (int i = 0; i <= n; i++) fast = fast.next;
            while (fast != null) { fast = fast.next; slow = slow.next; }
            slow.next = slow.next.next;
            return dummy.next;
        }
    }
  3. 3

    Merge Sorted Array in place into the first array which has trailing space.

    easy

    Fill from the back: with pointers at the last real element of each array and at the last slot of the first array, place the larger value at the end and move that pointer left. Remaining elements of the second array are copied at the end. Time O(m + n), space O(1). Mistake: merging from the front, which overwrites values not yet processed.

    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 {
        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--];
            }
        }
    }
  4. 4

    Binary Tree Right Side View.

    medium

    BFS by level and record the last node of each level, which is what you see from the right. Alternatively DFS visiting right before left and recording the first node at each new depth. Time O(n), space O(w) for the widest level. Mistake: only following right children, which misses levels where the right branch is shorter than the left.

    python
    from collections import deque
    def right_view(root):
        if not root: return []
        res, q = [], deque([root])
        while q:
            for _ in range(len(q)):
                n = q.popleft()
                last = n
                if n.left: q.append(n.left)
                if n.right: q.append(n.right)
            res.append(last.val)
        return res
    java
    import java.util.*;
    class TreeNode { int val; TreeNode left, right; }
    class Solution {
        List<Integer> rightView(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            if (root == null) return res;
            Deque<TreeNode> q = new ArrayDeque<>(); q.add(root);
            while (!q.isEmpty()) {
                int sz = q.size();
                for (int i = 0; i < sz; i++) {
                    TreeNode n = q.poll();
                    if (i == sz - 1) res.add(n.val);
                    if (n.left != null) q.add(n.left);
                    if (n.right != null) q.add(n.right);
                }
            }
            return res;
        }
    }
  5. 5

    Diameter of a Binary Tree (longest path in edges between any two nodes).

    medium

    For each node, the longest path through it is left height plus right height. Compute heights with a post-order DFS and update a global maximum at every node. Time O(n), space O(h). Mistake: assuming the longest path goes through the root, or recomputing heights at each node, which is O(n squared) for a skewed tree.

    python
    def diameter(root):
        best = 0
        def h(n):
            nonlocal best
            if not n: return 0
            l, r = h(n.left), h(n.right)
            best = max(best, l + r)
            return 1 + max(l, r)
        h(root)
        return best
    java
    class TreeNode { int val; TreeNode left, right; }
    class Solution {
        int best = 0;
        int diameter(TreeNode root) { h(root); return best; }
        private int h(TreeNode n) {
            if (n == null) return 0;
            int l = h(n.left), r = h(n.right);
            best = Math.max(best, l + r);
            return 1 + Math.max(l, r);
        }
    }
  6. 6

    Longest Substring with At Most K Distinct Characters.

    medium

    Sliding window with a count map. Expand the right edge adding characters; while the map has more than k distinct characters, shrink from the left, removing keys whose count reaches zero. Track the maximum window. Time O(n), space O(k). Mistake: forgetting to delete zero-count keys, so the map size overstates distinct characters.

    python
    def longest_k_distinct(s, k):
        cnt, l, best = {}, 0, 0
        for r, c in enumerate(s):
            cnt[c] = cnt.get(c, 0) + 1
            while len(cnt) > k:
                cnt[s[l]] -= 1
                if cnt[s[l]] == 0: del cnt[s[l]]
                l += 1
            best = max(best, r - l + 1)
        return best
    java
    import java.util.*;
    class Solution {
        int longestKDistinct(String s, int k) {
            Map<Character, Integer> cnt = new HashMap<>();
            int l = 0, best = 0;
            for (int r = 0; r < s.length(); r++) {
                cnt.merge(s.charAt(r), 1, Integer::sum);
                while (cnt.size() > k) {
                    char c = s.charAt(l++);
                    if (cnt.merge(c, -1, Integer::sum) == 0) cnt.remove(c);
                }
                best = Math.max(best, r - l + 1);
            }
            return best;
        }
    }
  7. 7

    Rotate List to the right by k places.

    medium

    Find the length and the tail, make the list circular, then break it at the node n - (k mod n) steps from the head. Time O(n), space O(1). Always reduce k modulo the length first. Mistake: forgetting k can exceed the length and that an empty or single-node list needs an early return.

    python
    def rotate_right(head, k):
        if not head or not head.next or k == 0: return head
        n, tail = 1, head
        while tail.next:
            tail = tail.next; n += 1
        k %= n
        if k == 0: return head
        tail.next = head
        new_tail = head
        for _ in range(n - k - 1): new_tail = new_tail.next
        new_head = new_tail.next
        new_tail.next = None
        return new_head
    java
    class ListNode { int val; ListNode next; }
    class Solution {
        ListNode rotateRight(ListNode head, int k) {
            if (head == null || head.next == null || k == 0) return head;
            int n = 1; ListNode tail = head;
            while (tail.next != null) { tail = tail.next; n++; }
            k %= n;
            if (k == 0) return head;
            tail.next = head;
            ListNode nt = head;
            for (int i = 0; i < n - k - 1; i++) nt = nt.next;
            ListNode nh = nt.next; nt.next = null;
            return nh;
        }
    }
  8. 8

    First Missing Positive in O(n) time and O(1) extra space.

    hard

    Place each value v in range 1..n at index v - 1 by swapping until the current position holds a correct value or an out-of-range or duplicate one. Then the first index i where a[i] is not i + 1 gives answer i + 1, or n + 1 if all match. Each swap puts a value in its final place, so total time is O(n). Mistake: using a while loop without the check a[a[i]-1] != a[i], causing an infinite loop on duplicates.

    python
    def first_missing_positive(a):
        n = len(a)
        for i in range(n):
            while 1 <= a[i] <= n and a[a[i] - 1] != a[i]:
                j = a[i] - 1
                a[i], a[j] = a[j], a[i]
        for i in range(n):
            if a[i] != i + 1: return i + 1
        return n + 1
    java
    class Solution {
        int firstMissingPositive(int[] a) {
            int n = a.length;
            for (int i = 0; i < n; i++)
                while (a[i] >= 1 && a[i] <= n && a[a[i] - 1] != a[i]) {
                    int j = a[i] - 1, t = a[i]; a[i] = a[j]; a[j] = t;
                }
            for (int i = 0; i < n; i++) if (a[i] != i + 1) return i + 1;
            return n + 1;
        }
    }
  9. 9

    Find the Duplicate Number in an array of n + 1 integers from 1 to n, without modifying it.

    medium

    Treat the array as a linked list where index i points to a[i]. Because a value repeats, the list has a cycle, and the cycle's entry point is the duplicate. Use Floyd's algorithm: find the meeting point, then restart one pointer at the start and move both one step to find the entrance. Time O(n), space O(1). Mistake: sorting or using a set, which modifies the array or uses O(n) space.

    python
    def find_duplicate(a):
        slow = fast = a[0]
        while True:
            slow, fast = a[slow], a[a[fast]]
            if slow == fast: break
        slow = a[0]
        while slow != fast:
            slow, fast = a[slow], a[fast]
        return slow
    java
    class Solution {
        int findDuplicate(int[] a) {
            int slow = a[0], fast = a[0];
            do { slow = a[slow]; fast = a[a[fast]]; } while (slow != fast);
            slow = a[0];
            while (slow != fast) { slow = a[slow]; fast = a[fast]; }
            return slow;
        }
    }
  10. 10

    3Sum Closest: the triple sum nearest to a target.

    medium

    Sort, then for each first element use two pointers on the remainder, moving left or right depending on whether the current sum is below or above the target and tracking the closest sum; return immediately on an exact match. Time O(n squared), space O(1). Mistake: tracking the smallest sum instead of the smallest absolute difference.

    python
    def three_sum_closest(a, t):
        a.sort(); best = a[0] + a[1] + a[2]
        for i in range(len(a) - 2):
            l, r = i + 1, len(a) - 1
            while l < r:
                s = a[i] + a[l] + a[r]
                if abs(s - t) < abs(best - t): best = s
                if s < t: l += 1
                elif s > t: r -= 1
                else: return s
        return best
    java
    import java.util.*;
    class Solution {
        int threeSumClosest(int[] a, int t) {
            Arrays.sort(a);
            int best = a[0] + a[1] + a[2];
            for (int i = 0; i < a.length - 2; i++) {
                int l = i + 1, r = a.length - 1;
                while (l < r) {
                    int s = a[i] + a[l] + a[r];
                    if (Math.abs(s - t) < Math.abs(best - t)) best = s;
                    if (s < t) l++; else if (s > t) r--; else return s;
                }
            }
            return best;
        }
    }

Frequently asked questions

What difficulty are Oracle coding questions?

Commonly practised at easy to medium level on core data structures, with some harder follow-ups. Aim for clean, correct code.

Which languages are allowed?

This depends on the platform or interviewer. Python and Java are common, and all ten problems here have both.

Should I memorise these solutions?

No. Learn the patterns, such as two pointers, dummy nodes and Floyd's cycle method, so you can adapt.

Sources

Related guides

More in Company Interview Questions

See all in this section