← Apple Interview Insights

Apple·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Jul 2026

Summary

Apple software engineer coding round with four problems spanning trees, date manipulation, signal processing, and sorting. Nothing too exotic but the palindrome date one took me longer than I'd like to admit.

Questions Asked (4)

Q1

Given the root of a binary tree, return the values of all leaf nodes in left-to-right order.

Algorithms & Data Structures
Author's notes

Pretty standard.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a depth-first traversal (pre-order) that visits the left child before the right child, collecting values only when a node has no children. This ensures leaf nodes are recorded in left-to-right order. Discuss iterative and recursive implementations, and analyze time and space complexity.

Pro tip: At Apple, interviewers value clean, efficient code and awareness of edge cases. Mention that an iterative solution avoids recursion depth limits for skewed trees, and always test with an empty tree, a single node, and a tree with only left or right children.

1. Clarify the problem and constraints

Confirm the definition of a leaf node (no children) and that the tree is not necessarily balanced. Ask about input size to discuss recursion limits.

2. Choose a traversal strategy

Select a depth-first search (pre-order) that processes left before right. Explain why this yields left-to-right leaf order.

3. Implement the solution

Write a recursive function that checks if a node is a leaf; if so, add its value to the result list. Otherwise, recurse on left then right. Optionally, present an iterative version using a stack.

4. Analyze complexity and edge cases

State O(n) time and O(h) space for recursion (or O(n) for iterative stack). Discuss edge cases: empty tree, single node, skewed tree, and trees with varying depths.

5. Test with examples

Walk through a sample tree to verify the output order. Mention that you would write unit tests covering the edge cases.

Key Points to Mention

  • Definition of a leaf node: a node with no left or right child.
  • Left-to-right order is naturally achieved by visiting left subtree before right subtree in a DFS.
  • Recursive vs. iterative implementation trade-offs (stack overflow risk, code clarity).
  • Time complexity O(n) and space complexity O(h) for recursion, where h is tree height.
  • Edge cases: empty tree, single node, skewed tree, and trees with only left or right children.
  • Potential follow-up: handling very deep trees by using an iterative approach with an explicit stack.

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

Q2

Given a date in YYYY-MM-DD format, find the most recent earlier date whose YYYYMMDD string representation is a palindrome. Describe the algorithm precisely, including leap year handling.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one is sneakier than it looks.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: we need the latest palindrome date strictly before the given date, where the palindrome is formed by the concatenated YYYYMMDD digits. The key insight is that any 8-digit palindrome is fully determined by its first 4 digits (the year), so we can iterate over candidate years, construct the corresponding palindrome date, and check validity with leap year rules. Then pick the largest valid palindrome date that is less than the input.

Pro tip: Mention that you can avoid iterating day-by-day by generating candidates from the year, and explicitly handle the edge case where the constructed palindrome date falls in the same year as the input but is not earlier—then decrement the year. Also note that dates before 1000 AD would have a 7-digit representation, but the problem likely assumes modern dates; clarify this assumption.

1. Clarify and define the problem

Confirm the input format, that the palindrome is over the 8-character YYYYMMDD string, and that we need the most recent date strictly earlier than the given date. Ask about date range (e.g., year >= 1000) to avoid 7-digit edge cases.

2. Derive the palindrome construction

For a given year Y (4 digits), the palindrome date is formed by mirroring the year: YYYYMMDD where MMDD is the reverse of YYYY. So the month is the reverse of the last two digits of the year, and the day is the reverse of the first two digits.

3. Validate the constructed date

Check if the month is between 1 and 12, and the day is valid for that month and year, including leap year rules (divisible by 4, except centuries unless divisible by 400).

4. Search for the most recent valid palindrome

Start from the year of the input date, construct the palindrome, and if it's valid and strictly earlier than the input, return it. Otherwise, decrement the year and repeat until a valid palindrome is found.

5. Analyze complexity and edge cases

Discuss time complexity (O(1) since at most a few years back) and edge cases: input date itself is a palindrome (must return earlier), leap years, and dates near year boundaries.

Key Points to Mention

  • Palindrome structure: YYYYMMDD is a palindrome iff YYYY reversed equals MMDD, so the date is determined solely by the year.
  • Leap year rules: divisible by 4, but not by 100 unless also by 400; affects February 29 validity.
  • Validity checks: month 01-12, day 01-31 depending on month, with February having 28 or 29 days.
  • Search strategy: iterate backwards from the input year, construct candidate palindrome, validate, and return first valid one that is strictly earlier.
  • Edge case: if the constructed palindrome for the input year is not earlier than the input date, skip to the previous year.
  • Complexity: constant time in practice because the maximum gap between palindrome dates is small (at most a few years).

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

Q3

Implement a rising-edge detector: given an array of 0/1 samples, output an array where index i is 1 only if the previous sample was 0 and the current sample is 1. The first output is always 0.

Algorithms & Data Structures
Author's notes

Basically a one-liner once you see it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: confirm input/output types, edge cases (empty array, single element), and that the first output is always 0. Then propose a simple O(n) single-pass solution that compares each element with its predecessor, handling the first index separately. Finally, discuss potential optimizations or variations, such as in-place modification or bitwise operations.

Pro tip: Mention that this is a common building block in digital signal processing and hardware verification, and that Apple often values clean, efficient code with clear edge-case handling. Also, consider asking if the input can be modified in-place to save memory.

1. Clarify requirements and edge cases

Ask about input size, data types, and whether the output should be a new array or can modify the input. Confirm edge cases: empty array, single element, and arrays with no rising edges.

2. Outline the algorithm

Explain that you'll iterate through the array starting from index 1, comparing each element with the previous one. If previous is 0 and current is 1, set output to 1; otherwise 0. Set output[0] = 0.

3. Write clean code

Implement the solution in your preferred language, using clear variable names and handling edge cases. For example, in Python: output = [0]*len(samples); for i in range(1, len(samples)): output[i] = 1 if samples[i-1]==0 and samples[i]==1 else 0.

4. Test with examples

Walk through a few test cases: [0,1,0,1] -> [0,1,0,1]; [1,0,1] -> [0,0,1]; [0,0,0] -> [0,0,0]; empty array -> empty array. Verify correctness.

5. Discuss complexity and optimizations

State that time complexity is O(n) and space is O(n) for the output. Mention that if in-place modification is allowed, space can be O(1) by updating the input array from the end or using a temporary variable.

Key Points to Mention

  • Time and space complexity analysis: O(n) time, O(n) space for output (or O(1) if in-place).
  • Edge cases: empty array, single element, all zeros, all ones, alternating patterns.
  • The first output is always 0, so start iteration from index 1.
  • Comparison condition: previous == 0 and current == 1.
  • Potential variations: detect falling edge, both edges, or use bitwise operations for efficiency.
  • Real-world applications: signal processing, hardware design, event detection.

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

Q4

Implement a sorting function for an array of integers. Any correct O(n log n) sort is acceptable; if you use bubble sort, state its complexity.

Algorithms & Data Structures
Author's notes

Went with merge sort.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Choose a well-known O(n log n) sorting algorithm like merge sort or quicksort, and implement it cleanly with clear variable names and comments. If you opt for a simpler O(n^2) algorithm like bubble sort, explicitly state its time complexity and explain why you chose it despite the inefficiency. Focus on correctness, edge cases, and discussing trade-offs.

Pro tip: At Apple, attention to detail and performance are paramount. After implementing, briefly analyze the algorithm's stability, in-place nature, and worst-case performance, and mention how you would test it with edge cases like empty arrays, duplicates, and already sorted data.

1. Clarify requirements

Confirm the input format (array of integers), expected output (sorted array), and any constraints like memory or stability. Ask if in-place sorting is required or if additional space is acceptable.

2. Select an algorithm

Choose an O(n log n) algorithm such as merge sort or quicksort. Briefly justify your choice based on trade-offs (e.g., merge sort for stability, quicksort for average-case speed).

3. Implement the algorithm

Write clean, modular code with clear variable names and comments. Handle edge cases like empty arrays or single elements. If using bubble sort, explicitly state its O(n^2) complexity.

4. Analyze complexity and trade-offs

State the time and space complexity of your solution. Discuss stability, in-place vs. out-of-place, and worst-case scenarios (e.g., quicksort's O(n^2) worst case).

5. Test and validate

Walk through a small example and mention testing with edge cases: empty array, duplicates, negative numbers, already sorted, reverse sorted. Consider writing unit tests.

Key Points to Mention

  • Time complexity: O(n log n) for merge sort/quicksort, O(n^2) for bubble sort
  • Space complexity: O(n) for merge sort, O(log n) for quicksort (recursion stack)
  • Stability: merge sort is stable, quicksort is not
  • In-place vs. out-of-place: quicksort is in-place, merge sort is not
  • Edge cases: empty array, single element, duplicates, already sorted
  • Trade-offs: quicksort's average-case speed vs. worst-case O(n^2); merge sort's guaranteed O(n log n) but higher space

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