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.
| Problems | 10, easy to hard |
|---|---|
| Languages | Python and Java |
| Patterns | Linked lists, trees, sliding window, in-place arrays |
| Rounds described | Two 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
- Jumping to code before confirming input size and edge cases.
- Pointer bugs in linked lists: missing dummy nodes when the head may change.
- Choosing an O(n squared) approach when a linear pass exists.
- Forgetting to reduce k modulo n in rotation problems.
- Not stating time and space complexity.
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 Reverse Words in a String (collapse extra spaces).
easySplit 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.
pythondef reverse_words(s): return ' '.join(reversed(s.split()))javaimport java.util.*; class Solution { String reverseWords(String s) { String[] w = s.trim().split("\\s+"); Collections.reverse(Arrays.asList(w)); return String.join(" ", w); } }2 Remove the Nth Node From the End of a linked list in one pass.
mediumUse 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.
pythondef 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.nextjavaclass 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 Merge Sorted Array in place into the first array which has trailing space.
easyFill 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.
pythondef 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 -= 1javaclass 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 Binary Tree Right Side View.
mediumBFS 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.
pythonfrom 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 resjavaimport 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 Diameter of a Binary Tree (longest path in edges between any two nodes).
mediumFor 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.
pythondef 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 bestjavaclass 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 Longest Substring with At Most K Distinct Characters.
mediumSliding 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.
pythondef 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 bestjavaimport 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 Rotate List to the right by k places.
mediumFind 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.
pythondef 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_headjavaclass 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 First Missing Positive in O(n) time and O(1) extra space.
hardPlace 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.
pythondef 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 + 1javaclass 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 Find the Duplicate Number in an array of n + 1 integers from 1 to n, without modifying it.
mediumTreat 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.
pythondef 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 slowjavaclass 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 3Sum Closest: the triple sum nearest to a target.
mediumSort, 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.
pythondef 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 bestjavaimport 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.
What should I read next?
How to crack the Oracle interview and how to crack the DSA coding round.
Sources
Related guides
- Oracle Interview Questions and Answers for SDE Roles
- Salesforce Coding Round Questions with Solutions in Python and Java
- Microsoft Coding Round Questions with Solutions in Python and Java
- Linked list interview questions with worked solutions
- Binary Tree Interview Questions and Answers
- Sliding window interview questions with worked solutions
More in Company Interview Questions
- Oracle Interview Questions and Answers for SDE Roles
- PayPal coding round questions with solutions
- PayPal interview questions with answers (software engineer, India)
- Paytm Coding Round Questions with Solutions
- Paytm Interview Questions and Answers
- Persistent Systems Coding Round Questions with Solutions
- Persistent Systems Interview Questions for Freshers
- PhonePe Coding Round Questions with Solutions
- PhonePe Interview Questions and Answers
- Postman Coding Round Questions With Solutions
- Postman Interview Questions and Answers
- Publicis Sapient Coding Round Questions