Skip to content

Apple Coding Questions

Practice 25+ Apple coding interview questions with solutions in multiple languages. Apple focuses on clean code, optimal solutions, and edge case handling.

Arrays & strings

1. Two sum

Find indices of two numbers that add up to target.

Input: nums = [2,7,11,15], target = 9
Output: [0,1]

2. Container with most water

Find two lines that form container with most water.

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

3. Longest substring without repeating characters

Find length of longest substring without repeating characters.

Input: “abcabcbb”
Output: 3 (“abc”)

4. Product of array except self

Return array where each element is product of all other elements.

Input: [1,2,3,4]
Output: [24,12,8,6]

5. Trapping rain water

Calculate water trapped after raining.

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

Trees & graphs

6. Validate binary search tree

Check if binary tree is a valid BST.

7. Binary tree maximum path sum

Find maximum path sum in binary tree. Path can start and end at any node.

Input: [-10,9,20,null,null,15,7]
Output: 42 (15 + 20 + 7)

8. Number of islands

Count number of islands in 2D grid.

9. Lowest common ancestor

Find LCA of two nodes in binary tree.

10. Course schedule

Check if all courses can be finished given prerequisites.

Dynamic programming

11. Longest increasing subsequence

Find length of longest increasing subsequence.

Input: [10,9,2,5,3,7,101,18]
Output: 4

12. Coin change

Find minimum coins needed to make amount.

Input: coins = [1,2,5], amount = 11
Output: 3 (5+5+1)

13. Word break

Can string be segmented into dictionary words?

Input: s = “leetcode”, wordDict = [“leet”,“code”]
Output: true

14. House robber

Max money without robbing adjacent houses.

Input: [2,7,9,3,1]
Output: 12 (2+9+1)

15. Edit distance

Minimum operations to convert word1 to word2.

Input: word1 = “horse”, word2 = “ros”
Output: 3

System Design & design patterns

16. Lru cache

Design LRU Cache with O(1) get and put.

17. Design twitter

Design simplified Twitter with follow, unfollow, post, getNewsFeed.

Advanced problems

18. Merge k sorted lists

import heapq
def merge_k_lists(lists):
heap = []
for i, lst in enumerate(lists):
if lst:
heapq.heappush(heap, (lst.val, i, lst))
dummy = ListNode(0)
current = dummy
while heap:
val, i, node = heapq.heappop(heap)
current.next = node
current = current.next
if node.next:
heapq.heappush(heap, (node.next.val, i, node.next))
return dummy.next

19. Serialize and deserialize binary tree

class Codec:
def serialize(self, root):
def dfs(node):
if not node:
return ['null']
return [str(node.val)] + dfs(node.left) + dfs(node.right)
return ','.join(dfs(root))
def deserialize(self, data):
nodes = iter(data.split(','))
def dfs():
val = next(nodes)
if val == 'null':
return None
node = TreeNode(int(val))
node.left = dfs()
node.right = dfs()
return node
return dfs()

20. Find median from data stream

import heapq
class MedianFinder:
def __init__(self):
self.small = [] # max heap (negated)
self.large = [] # min heap
def addNum(self, num):
heapq.heappush(self.small, -num)
heapq.heappush(self.large, -heapq.heappop(self.small))
if len(self.large) > len(self.small):
heapq.heappush(self.small, -heapq.heappop(self.large))
def findMedian(self):
if len(self.small) > len(self.large):
return -self.small[0]
return (-self.small[0] + self.large[0]) / 2

Comments & Suggestions

Similar companies

Google · Microsoft · Meta · Amazon · Netflix · TCS