Solve 15 common Apple coding questions
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
This set evaluates algorithmic problem-solving and data-structure competence, including mastery of hash tables, linked lists, stacks, queues, heaps, graphs, sliding-window and two-pointer techniques, complexity analysis, and in-place transformations.
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Loading coding console...
Quick Answer: This set evaluates algorithmic problem-solving and data-structure competence, including mastery of hash tables, linked lists, stacks, queues, heaps, graphs, sliding-window and two-pointer techniques, complexity analysis, and in-place transformations.
Input: (2, [('put', 1, 1), ('put', 2, 2), ('get', 1), ('put', 3, 3), ('get', 2), ('put', 4, 4), ('get', 1), ('get', 3), ('get', 4)])
Expected Output: [None, None, 1, None, -1, None, -1, 3, 4]
Explanation: Standard LRU behavior with evictions of keys 2 and 1.
Input: (1, [('put', 2, 1), ('put', 2, 2), ('get', 2), ('put', 1, 1), ('get', 2)])
Expected Output: [None, None, 2, None, -1]
Explanation: Updating an existing key should refresh its recency.
Input: (0, [('put', 1, 1), ('get', 1)])
Expected Output: [None, -1]
Explanation: A zero-capacity cache never stores anything.
Input: (2, [('get', 5)])
Expected Output: [-1]
Explanation: Getting a missing key returns -1.
Input: ([('insert', 1), ('remove', 2), ('insert', 2), ('getRandom',), ('remove', 1), ('insert', 2), ('getRandom',)], 0)
Expected Output: [True, False, True, 2, True, False, 2]
Explanation: This follows the standard insert/remove behavior, with deterministic random picks.
Input: ([('getRandom',), ('insert', 5), ('getRandom',), ('remove', 5), ('getRandom',)], 7)
Expected Output: [None, True, 5, True, None]
Explanation: Calling `getRandom` on an empty set returns None.
Input: ([('insert', 10), ('insert', 20), ('insert', 30), ('getRandom',), ('remove', 20), ('getRandom',), ('getRandom',)], 1)
Expected Output: [True, True, True, 10, True, 30, 10]
Explanation: After removing 20, the last value is swapped into its place.
Input: ([('insert', 1), ('insert', 1), ('remove', 2), ('remove', 1)], 5)
Expected Output: [True, False, False, True]
Explanation: Duplicate inserts and removing missing values should return False.
Input: ([('insert', 'apple'), ('search', 'apple'), ('search', 'app'), ('startsWith', 'app'), ('insert', 'app'), ('search', 'app')],)
Expected Output: [None, True, False, True, None, True]
Explanation: A prefix is not automatically a complete word until inserted.
Input: ([('search', ''), ('startsWith', ''), ('insert', ''), ('search', ''), ('startsWith', '')],)
Expected Output: [False, True, None, True, True]
Explanation: The empty prefix matches trivially, but the empty word must be inserted before search succeeds.
Input: ([('insert', 'cat'), ('insert', 'car'), ('startsWith', 'ca'), ('search', 'cap'), ('startsWith', 'dog')],)
Expected Output: [None, None, True, False, False]
Explanation: The trie can share the `ca` prefix.
Input: ([1, 1, 1, 2, 2, 3], 2)
Expected Output: [1, 2]
Explanation: 1 appears three times and 2 appears twice.
Input: ([4, 4, 1, 1, 2, 2], 2)
Expected Output: [1, 2]
Explanation: All three values tie, so smaller values come first.
Input: ([], 0)
Expected Output: []
Explanation: Empty input with k = 0 returns an empty list.
Input: ([5], 1)
Expected Output: [5]
Explanation: A single element is trivially the most frequent.
Input: ([1, 2, 3, 4],)
Expected Output: [24, 12, 8, 6]
Explanation: Each position gets the product of all other values.
Input: ([-1, 1, 0, -3, 3],)
Expected Output: [0, 0, 9, 0, 0]
Explanation: The single zero makes only one position non-zero.
Input: ([5],)
Expected Output: [1]
Explanation: The product of all elements except itself is the empty product, defined here as 1.
Input: ([0, 0],)
Expected Output: [0, 0]
Explanation: With two zeros, every position becomes zero.
Input: ([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1],)
Expected Output: 6
Explanation: This is the classic example with multiple pockets.
Input: ([4, 2, 0, 3, 2, 5],)
Expected Output: 9
Explanation: Water is trapped between several higher bars.
Input: ([],)
Expected Output: 0
Explanation: No bars means no trapped water.
Input: ([1, 2, 3],)
Expected Output: 0
Explanation: Strictly increasing heights cannot trap water.
Input: ([1, 8, 6, 2, 5, 4, 8, 3, 7],)
Expected Output: 49
Explanation: The best pair is height 8 at index 1 and height 7 at index 8.
Input: ([1, 1],)
Expected Output: 1
Explanation: Only one container is possible.
Input: ([4, 3, 2, 1, 4],)
Expected Output: 16
Explanation: The widest good pair uses both 4s.
Input: ([5],)
Expected Output: 0
Explanation: At least two lines are required.
Input: ('abcabcbb',)
Expected Output: 3
Explanation: The longest substring without repeats is `abc`.
Input: ('bbbbb',)
Expected Output: 1
Explanation: Any valid substring can only contain one `b`.
Input: ('',)
Expected Output: 0
Explanation: An empty string has length 0.
Input: ('abba',)
Expected Output: 2
Explanation: The best substring is `ab` or `ba`.
Input: (['eat', 'tea', 'tan', 'ate', 'nat', 'bat'],)
Expected Output: [['ate', 'eat', 'tea'], ['bat'], ['nat', 'tan']]
Explanation: The output is normalized by sorting within and across groups.
Input: ([],)
Expected Output: []
Explanation: No words means no groups.
Input: ([''],)
Expected Output: [['']]
Explanation: A single empty string forms one group.
Input: (['abc', 'bca', 'cab', 'foo', 'ofo', 'bar'],)
Expected Output: [['abc', 'bca', 'cab'], ['bar'], ['foo', 'ofo']]
Explanation: Each anagram class becomes one group.
Input: ('3[a2[c]]',)
Expected Output: 'accaccacc'
Explanation: `a2[c]` becomes `acc`, repeated 3 times.
Input: ('2[abc]3[cd]ef',)
Expected Output: 'abcabccdcdcdef'
Explanation: Decode each bracketed segment and concatenate.
Input: ('10[a]',)
Expected Output: 'aaaaaaaaaa'
Explanation: Multi-digit repeat counts must be handled correctly.
Input: ('',)
Expected Output: ''
Explanation: Empty input decodes to an empty string.
Input: (['11000', '11000', '00100', '00011'],)
Expected Output: 3
Explanation: There are three disconnected land masses.
Input: (['11110', '11010', '11000', '00000'],)
Expected Output: 1
Explanation: All land cells belong to one island.
Input: ([],)
Expected Output: 0
Explanation: An empty grid contains no islands.
Input: (['1'],)
Expected Output: 1
Explanation: A single land cell is one island.
Input: (2, [[1, 0]])
Expected Output: True
Explanation: Take course 0, then course 1.
Input: (2, [[1, 0], [0, 1]])
Expected Output: False
Explanation: The prerequisites form a cycle.
Input: (4, [[1, 0], [2, 0], [3, 1], [3, 2]])
Expected Output: True
Explanation: A valid topological ordering exists.
Input: (0, [])
Expected Output: True
Explanation: With no courses, finishing all courses is trivially possible.
Input: ([[1, 4, 5], [1, 3, 4], [2, 6]],)
Expected Output: [1, 1, 2, 3, 4, 4, 5, 6]
Explanation: This is the classic merge-k-lists example.
Input: ([],)
Expected Output: []
Explanation: No lists means no output values.
Input: ([[], [1], [], [0, 2]],)
Expected Output: [0, 1, 2]
Explanation: Empty lists should be ignored.
Input: ([[5]],)
Expected Output: [5]
Explanation: A single one-node list stays unchanged.
Input: ([[1, 2, 3], [4, 5, 6], [7, 8, 9]],)
Expected Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
Explanation: This is the standard 3x3 example.
Input: ([[1]],)
Expected Output: [[1]]
Explanation: A 1x1 matrix is unchanged.
Input: ([],)
Expected Output: []
Explanation: An empty matrix remains empty.
Input: ([[1, 2], [3, 4]],)
Expected Output: [[3, 1], [4, 2]]
Explanation: The 2x2 rotation is straightforward to verify.
Input: ([2, 1, 4, 7, 3, 2, 5],)
Expected Output: 5
Explanation: The longest mountain is `[1, 4, 7, 3, 2]`.
Input: ([2, 2, 2],)
Expected Output: 0
Explanation: Flat segments cannot form a mountain.
Input: ([1, 3, 2],)
Expected Output: 3
Explanation: This whole array is a mountain.
Input: ([],)
Expected Output: 0
Explanation: No elements means no mountain.
Input: ([1, 2, 3, 4, 5],)
Expected Output: 0
Explanation: A purely increasing array is not a mountain.