← Oracle Interview Insights

Oracle·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Oracle SWE interview with a classic array partitioning problem. Pretty algorithmic, nothing flashy, but the follow-ups pushed harder than I expected.

Questions Asked (3)

Q1

Given an integer array, partition it so all even numbers appear before all odd numbers. You can swap any two elements at any positions. What is the minimum number of swaps needed?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Two pointers from opposite ends, left skips past evens, right skips past odds, and when both land on violations you swap and count it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the goal is to partition the array into evens followed by odds, and that any two elements can be swapped. Then, use a two-pointer technique to count the number of misplaced elements (odds in the even section and evens in the odd section) and determine the minimum swaps as the maximum of these two counts. Explain that each swap can fix at most two misplaced elements, so the minimum swaps equals the number of misplaced evens (or odds) in the wrong section.

Pro tip: Mention that the minimum swaps equals the number of misplaced elements of one type, and that this is optimal because each swap can correct at most one misplaced element of each type. Also, note that if the array is already partitioned, the answer is 0.

1. Clarify the problem

Confirm that the partition requires all even numbers before all odd numbers, and that we can swap any two elements. Ask if the order within evens or odds matters (usually not).

2. Identify misplaced elements

Scan the array to find the number of odd numbers in the even section (left part) and even numbers in the odd section (right part). These are the elements that need to be swapped.

3. Determine minimum swaps

The minimum number of swaps is the maximum of the two counts, because each swap can fix one misplaced even and one misplaced odd simultaneously. If counts are unequal, extra swaps will involve swapping two misplaced elements of the same type, but that still counts as one swap per pair.

4. Provide an example

Walk through a small example, such as [2,1,4,3], to illustrate the counting and swapping process, showing that the minimum swaps is 1.

5. Discuss complexity and edge cases

State that the algorithm runs in O(n) time and O(1) space. Mention edge cases: all evens, all odds, already partitioned, and empty array.

Key Points to Mention

  • Two-pointer technique to count misplaced elements in one pass.
  • Minimum swaps equals the number of misplaced evens (or odds) in the wrong section.
  • Each swap can fix at most two misplaced elements, so the count is optimal.
  • Time complexity O(n) and space complexity O(1).
  • Edge cases: already partitioned, all evens, all odds, empty array.
  • The order of evens and odds within their groups does not affect the minimum swaps.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

How would you modify the approach to produce a stable partition, preserving the original relative order within the even and odd groups?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I got a little stuck.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the original approach (likely a two-pointer swap that is unstable) and explain why it fails to preserve relative order. Then propose a stable alternative such as using an auxiliary array to collect evens and odds in order, or a stable in-place algorithm like stable partitioning via rotation. Discuss trade-offs between time/space complexity and stability.

Pro tip: Mention that stability often requires extra space or more complex in-place algorithms, and that in production code, clarity and maintainability may outweigh the cost of O(n) extra space. Also, relate to real-world scenarios like sorting records by multiple keys where stability matters.

1. Clarify the original approach and its instability

Briefly describe the typical two-pointer swap method for partitioning even/odd numbers, and point out that swapping elements can change the relative order of elements within each group.

2. Define stable partition and its requirements

State that a stable partition preserves the original relative order of elements within each partition (evens and odds). Emphasize that this is a key property when the elements have associated data or when multiple sorting criteria are applied.

3. Propose a stable approach

Present one or more stable algorithms: e.g., using an auxiliary array to collect evens then odds in a single pass, or an in-place stable partition using rotations (like the block swap algorithm). Explain the steps clearly.

4. Analyze time and space complexity

Compare the auxiliary array method (O(n) time, O(n) space) with in-place stable methods (O(n log n) time, O(1) space for some algorithms). Discuss the trade-offs and when each might be appropriate.

5. Conclude with practical considerations

Summarize that the choice depends on constraints like memory availability, performance requirements, and code simplicity. Mention that in many cases, the auxiliary array approach is preferred for its simplicity and linear time.

Key Points to Mention

  • Definition of stable partition and why it matters (e.g., preserving order for equal keys in multi-key sorting).
  • The instability of the two-pointer swap method due to swapping distant elements.
  • Auxiliary array approach: single pass to collect evens and odds, then concatenate.
  • In-place stable partition algorithms (e.g., block swap, rotation-based) and their complexities.
  • Time and space complexity trade-offs: O(n) time with O(n) space vs. O(n log n) time with O(1) space.
  • Real-world applications: stable sorting, database operations, and maintaining order in UI lists.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

How would you generalize this partitioning logic to work with an arbitrary predicate instead of even/odd?

Algorithms & Data Structures
Author's notes

Easy pivot off the previous answer, just replace the even/odd check with a function parameter.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the generalized partitioning problem: given an array and a predicate function, rearrange elements so that those satisfying the predicate come before those that don't. Then describe a two-pointer or single-pass algorithm that uses the predicate to decide placement, and discuss how this abstraction improves code reusability and testability.

Pro tip: Mention that using a predicate function allows the partitioning logic to be decoupled from specific conditions, making it easier to unit test and adapt to new requirements without modifying the core algorithm.

1. Clarify the problem

Restate the goal: partition an array into two groups based on whether elements satisfy a given predicate, preserving or not preserving order as required.

2. Define the predicate interface

Explain that the predicate is a function that takes an element and returns a boolean, e.g., `bool pred(T element)`. This allows any condition to be plugged in.

3. Choose an algorithm

Describe a two-pointer approach (like Hoare's partition) or a stable single-pass approach (like Lomuto's) that uses the predicate to decide swaps or placements.

4. Analyze complexity and stability

Discuss time complexity (typically O(n)) and space complexity (O(1) for in-place). Mention whether the algorithm is stable and how that affects the choice.

5. Provide an example

Walk through a concrete example, such as partitioning numbers by `x -> x % 2 == 0`, to illustrate how the predicate drives the logic.

Key Points to Mention

  • Predicate as a first-class function or lambda, enabling separation of concerns.
  • Two-pointer technique for in-place partitioning with O(n) time and O(1) space.
  • Stability considerations: stable partitioning preserves relative order, which may require extra space or a different algorithm.
  • Generic programming: using templates or generics to support any type and predicate.
  • Edge cases: empty array, all elements satisfy predicate, none satisfy predicate.
  • Testing benefits: easily test with different predicates without changing partition code.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.