The Workspace
Swift DSA Lab
Master algorithms & data structures using idiomatic Swift. Practice challenges from Arrays to Dynamic Programming in a clean editor.
Track Your Solutions
Log in to store your completed challenges on your public portfolio badges.
Next Up
Hello Arrays Step 1 / 100 Level 1
Sum of Even Numbers
An array is an ordered list of values. You can step through it with a for-in loop and inspect each element. This is your first building block.
Solve Step 10 / 100 solved
Hello Arrays 0/15 solved
01 L1 Sum of Even Numbers ✓ 02 L1 Count Positive Numbers ✓ 03 L1 Largest Element ✓ 04 L1 Smallest Element ✓ 05 L1 Count Target ✓ 06 L1 Range Sum ✓ 07 L1 Last Element ✓ 08 L1 Flip Signs ✓ 09 L1 Digit Sum ✓ 10 L1 Word Lengths ✓ 11 L1 Clamp Range ✓ 12 L1 Temperature Filter ✓ 13 L1 Array Average ✓ 14 L1 Contains Value ✓ 15 L1 Count Unique ✓
Sets & Hashing 0/13 solved
16 L1 Combine Arrays ✓ 17 L1 Starts With Capital ✓ 18 L1 Count Vowels ✓ 19 L1 Count Spaces ✓ 20 L1 Capitalize Word ✓ 21 L2 Second Largest ✓ 22 L2 Reverse Array ✓ 23 L2 Contains Duplicate ✓ 24 L2 Count Occurs Once ✓ 25 L2 Distinct Values ✓ 26 L2 Intersection Count ✓ 27 L2 First Repeated Value ✓ 28 L2 Common Characters ✓
Dictionaries & Counting 0/12 solved
Two Pointers 0/11 solved
41 L3 Contains Nearby Duplicate ✓ 42 L3 First Unique Character ✓ 43 L3 Valid Palindrome ✓ 44 L3 Paired Sum in a Sorted Array ✓ 45 L3 Sorted Squares ✓ 46 L3 Push Zeroes to the End ✓ 47 L3 Is a Subsequence ✓ 48 L3 Remove All Matching Values ✓ 49 L3 Apply Backspaces, Compare ✓ 50 L3 Merge Two Sorted Arrays ✓ 51 L3 Palindromic Number ✓
Sliding Window & Prefix 0/8 solved
Stacks 0/8 solved
Binary Search 0/8 solved
Linked Lists 0/8 solved
Recursion & Trees 0/8 solved
Dynamic Programming 0/6 solved
Elite Set 0/3 solved
Arrays & Hashing (35)
01 02 03 04 05 06 07 08 09 11 12 13 14 15 16 21 22 23 24 25 26 27 29 31 32 33 35 36 38 39 40 41 59 90 98
Sum of Even Numbers
Given an array of integers `nums`, return the sum of all the even numbers in it. If there are no even numbers, return `0`.
L1 Easy
Count Positive Numbers
Count how many integers in `nums` are strictly greater than `0`. A number like `0` itself does not count.
L1 Easy
Largest Element
Return the single largest integer in `nums`. You may assume the array always has at least one element.
L1 Easy
Smallest Element
Return the single smallest integer in `nums`. You may assume the array always has at least one element.
L1 Easy
Count Target
Count how many times `target` appears inside `nums`, then return that count.
L1 Easy
Range Sum
Given `nums`, add together every value from index `start` up to and including index `end`, and return the total. Both indexes are valid and `start` is never larger than `end`.
L1 Easy
Last Element
Return the last integer in `nums`. If the array is empty, return `-1`.
L1 Easy
Flip Signs
Return a new array where every integer in `nums` is negated: positive values become negative and negative values become positive. `0` stays `0`.
L1 Easy
Digit Sum
For a non-negative integer `n`, return the sum of its decimal digits. For `0`, return `0`.
L1 Easy
Clamp Range
Return a new array where every value in `nums` is clamped into the inclusive range `low...high`. Values already inside the range stay unchanged. You may assume `low` is at most `high`.
L1 Easy
Temperature Filter
You recorded the temperature each day in `temps`. Return how many readings are strictly greater than `threshold`.
L1 Easy
Array Average
Return the average of the integers in `nums` as a `Double`. If the array is empty, return `0`.
L1 Easy
Contains Value
Return `true` if `value` appears anywhere in `nums`, and `false` otherwise.
L1 Easy
Count Unique
Return how many distinct integers appear in `nums`. Duplicates should be counted only once.
L1 Easy
Combine Arrays
Return a single array formed by all of `a` followed by all of `b`.
L1 Easy
Second Largest
Return the second largest *distinct* value in `nums`. If the array has fewer than two distinct values, return `nil`.
L2 Easy
Reverse Array
Return a new array containing the values of `nums` in reverse order. The original array must not be modified.
L2 Easy
Contains Duplicate
Given an array of integers `nums`, report whether any number shows up more than once. Return `true` when a repeat exists, and `false` when every value is distinct.
L2 Easy
Count Occurs Once
Return how many distinct values in `nums` appear exactly once. Values that repeat do not count at all.
L2 Easy
Distinct Values
Return the distinct integers of `nums` in the order of their first appearance. Each value appears exactly once in the result.
L2 Easy
Intersection Count
Return how many distinct integers appear in both `a` and `b`. A shared value is counted once, even if repeated.
L2 Easy
First Repeated Value
Return the first value in `nums` that appears more than once. If every value is unique, return `nil`.
L2 Easy
Frequencies
Return a dictionary that maps each distinct integer in `nums` to the number of times it appears.
L2 Easy
Majority Element
Return the integer that appears more than `nums.count / 2` times. If no value is that frequent, return `-1`.
L2 Easy
Most Frequent Value
Return the integer that appears most often in `nums`. If several integers tie, return the smallest one. If `nums` is empty, return `nil`.
L2 Easy
Sum of Unique Values
Add together every integer in `nums` that appears exactly once. Values that repeat contribute nothing. If nothing qualifies, return `0`.
L2 Easy
Pair Count
Count every pair of indices `(i, j)` with `i < j` where `nums[i] + nums[j]` equals `target`. Different index pairs count separately.
L2 Easy
Find the Lone Number
Every integer in `nums` appears exactly twice, except for one integer that appears only once. Find and return that unique integer.
L2 Easy
Sorted Two Sum
The array `nums` is sorted from smallest to largest. Return `true` if two different entries add up to `target`, and `false` otherwise.
L2 Easy
Valid Anagram
Two strings are anagrams when one is just the other with its letters rearranged. Given `s` and `t`, return `true` if `t` is an anagram of `s`, and `false` otherwise.
L3 Easy
Two Sum
You are given an array of integers `nums` and a `target`. Two distinct numbers in `nums` add up to `target`. Return the indices of those two numbers, in any order. You may assume exactly one valid pair exists, and you may not use the same index twice.
L3 Easy
Contains Nearby Duplicate
Given `nums` and a distance `k`, return `true` if the same integer appears at two different indices that are at most `k` apart. Return `false` otherwise.
L3 Easy
Product Except Self
For each position `i`, compute the product of every element of `nums` except `nums[i]`, and return those values in order. Solve it in linear time and without using division.
L4 Easy
Group Anagrams
Given an array of strings `strs`, collect strings that are anagrams of each other into a single group and return all groups. The order of groups, and the order inside each group, does not matter.
L7 Medium
Merge Overlapping Intervals
Given an array of intervals `intervals` where each is `[start, end]` (inclusive), merge all overlapping intervals and return the resulting array of non-overlapping intervals.
L9 Hard
Two Pointers (10)
43 44 45 46 47 48 49 50 51 99
Valid Palindrome
A phrase reads the same forwards and backwards once you ignore anything that is not a letter or digit and ignore letter case. Given `s`, return `true` if it qualifies, and `false` otherwise.
L3 Easy
Paired Sum in a Sorted Array
You are given an array of integers `numbers`, sorted in non-decreasing order, and a `target`. Exactly two different entries add up to `target`. Return their 1-based positions (starting at `1`) as a two-element array, in any order. You may not reuse the same index twice.
L3 Easy
Sorted Squares
Given an array of integers `nums` sorted in non-decreasing order, return a new array holding the square of every element, arranged in non-decreasing order. Solve it in `O(n)` time.
L3 Easy
Push Zeroes to the End
Given an array `nums`, move every `0` to the end of the array while keeping the relative order of all non-zero numbers. Modify the array in place; return nothing.
L3 Easy
Is a Subsequence
Given strings `s` and `t`, return `true` if `s` is a subsequence of `t` — that is, you can delete some characters from `t` (possibly zero) and be left with exactly `s` — otherwise return `false`.
L3 Easy
Remove All Matching Values
Given an array `nums` and an integer `val`, remove every occurrence of `val` in place and return the number of remaining elements. The first `count` slots of `nums` should hold those remaining elements in their original relative order; ignore the rest.
L3 Easy
Apply Backspaces, Compare
A text editor treats '#' as a backspace: it deletes the character immediately before it (consecutive '#' delete more). Given two strings s and t, return true if both strings end up identical after all backspaces are applied. An empty string stays empty when its last character is deleted.
L3 Easy
Merge Two Sorted Arrays
You are given `nums1` (length `m + n` where the last `n` slots are zero placeholders) and `nums2` (length `n`), each sorted in non-decreasing order. Merge `nums2` into `nums1` in place so the whole array ends up sorted. The first `m` slots of `nums1` hold the real values. Return nothing.
L3 Easy
Palindromic Number
Given an integer `x`, return `true` if it reads the same forwards and backwards (ignoring the sign), otherwise `false`.
L3 Easy
Trapped Rain Water
Given an array of non-negative integers `height` where each value is a bar width of 1, return how many units of water can be trapped above the bars after it rains.
L10 Hard
Sliding Window (7)
52 53 54 55 56 57 58
Running Totals
Given an array `nums`, return an array where the element at each position is the sum of all elements up to and including that position.
L3 Easy
Largest Window Average
Given an array `nums` and an integer `k`, return the largest average value of any contiguous subarray of length exactly `k`. You can assume `1 <= k <= nums.count`.
L3 Easy
Longest Distinct Substring
Given a string `s`, return the length of the longest substring (contiguous block of characters) that contains every character at most once.
L4 Easy
Shortest Window Reaching the Target
Given an array `nums` of positive integers and a positive `target`, return the length of the shortest contiguous subarray whose sum is at least `target`. If no such subarray exists, return `0`.
L4 Easy
Longest Window With K Distinct Characters
Given a string `s` and an integer `k`, return the length of the longest substring that contains at most `k` distinct characters. If `k` is zero, no character may appear, so the answer is `0`.
L4 Easy
Prefix Sum Range Query
Given an array `nums` and two indices `left` and `right` (inclusive), return the sum of the elements between those indices. If the array is empty, return `0`.
L4 Easy
Best Time to Buy Stock
You know a stock price for each day in `prices`. You may buy on one day and sell on a strictly later day. Return the largest profit possible, or `0` if no profitable trade exists.
L4 Easy
Stack (8)
60 61 62 63 64 65 66 67
Scorekeeper
You are scoring a game round by round. The input is a list of strings where each entry is one of: an integer (a score for that round), "C" (remove the previous round's score), "D" (add twice the previous round's score), or "+" (add the sum of the two most recent scores). Return the sum of all scores left at the end.
L4 Easy
Maximum Nesting Depth
Given a string `s` containing balanced round brackets `()` alongside other characters, return the maximum nesting depth of the parentheses. Unbalanced strings are not possible in the input.
L4 Easy
Squash Duplicate Neighbors
Given a string `s`, repeatedly remove adjacent equal pairs. Removing a pair can make two new characters adjacent, which may form another pair — keep going until no adjacent equal pairs remain. Return the final string.
L4 Easy
Next Bigger Neighbor
Given an array `nums`, return an array where the value at each position is the next element to its right that is strictly greater than the value at that position, or `-1` if no such element exists.
L4 Easy
Remove Outer Parentheses
Given a string `s` made of valid ("primitive") parentheses, split it into primitives (each is a non-empty balanced block that cannot be split further) and return the concatenation of every primitive with its outermost pair of parentheses removed.
L4 Easy
Simplify a Filesystem Path
Given an absolute Unix-style path `path`, return its canonical form. Rules: a single slash separates directories, a trailing slash is dropped, "." refers to the current directory (ignore it), and ".." moves up one directory. The canonical path always starts with "/".
L4 Easy
Valid Parentheses
A string built from the brackets `()`, `{}`, and `[]` is balanced when every opener is closed by a matching bracket of the same kind, in the correct nested order. Given `s`, return `true` if it is balanced.
L5 Medium
Reverse Polish Calculator
Given an array of strings `tokens` formed from integers and the operators `+`, `-`, `*`, `/` in Reverse Polish notation, evaluate it and return the resulting integer. Division truncates toward zero.
L5 Medium
Binary Search (9)
68 69 70 71 72 73 74 75 100
Binary Search
`nums` is sorted from smallest to largest. Find `target` using binary search and return its index, or `-1` when it is not present.
L5 Medium
Search Insert Position
Given a sorted array of distinct integers `nums` and a `target`, return the index where `target` would be, keeping the order sorted if it were inserted. If `target` is already present, return its index.
L5 Medium
First Bad Version
You have `n` versions numbered `1` through `n`. A helper `isBad(version)` returns `true` once a version is broken, and stays broken for every later version. Return the smallest version that is broken.
L5 Medium
Guess the Number
A number between `1` and `n` is chosen. A guess helper returns `-1` if your guess is too low, `1` if too high, and `0` on a match. Return the chosen number.
L5 Medium
Integer Square Root
Given a non-negative integer x, return the largest integer whose square is less than or equal to x (the integer square root, rounded down). Do not use the built-in sqrt function.
L5 Medium
Smallest Letter Greater Than Target
Given a string `letters` of sorted lowercase letters (duplicates allowed) and a single-character `target`, return the smallest character in `letters` that is strictly greater than `target`. If none exists, wrap around and return the first character.
L5 Medium
Kth Missing Positive Integer
Given a sorted array of distinct positive integers `arr`, consider all positive integers not present in the array. Return the `k`th missing one.
L5 Medium
Find the Mountain Peak
Given an array `arr` that strictly increases up to one peak then strictly decreases, return the index of that peak. You may assume the array is a valid mountain (length at least 3, no plateau at the top).
L5 Medium
Median of Two Sorted Arrays
Given two sorted arrays `nums1` and `nums2`, return the median of the combined sorted array. For even combined length, the median is the average of the two middle values.
L10 Hard
Linked List (8)
76 77 78 79 80 81 82 83
Reverse Linked List
Given the head of a singly linked list whose nodes hold integers, reverse the list in place and return the new head.
L6 Medium
Merge Two Sorted Lists
The heads of two sorted linked lists are given. Merge the two lists so the result is a single sorted list containing every node of both. Return its head.
L6 Medium
Linked List Cycle
Some linked lists never end: following `next` pointers eventually revisits an earlier node in a loop. Given its head, return `true` if a cycle exists and `false` if the list terminates.
L6 Medium
Middle of a Linked List
Given the head of a singly linked list, return the node at the middle. If there are two middle nodes (even length), return the second one.
L6 Medium
Remove the Nth Node From the End
Given the head of a singly linked list, remove the node that is the `n`th from the end (1-based), and return the head of the updated list. You may assume `n` is always valid.
L6 Medium
Delete a Value From a List
Given the head of a singly linked list and an integer `val` that appears in the list, remove every node whose value equals `val` and return the head.
L6 Medium
Remove Duplicates From a Sorted List
Given the head of a sorted linked list, delete all nodes that appear more than once, keeping only one copy of each value. Return the head.
L6 Medium
Palindromic Linked List
Given the head of a singly linked list, return `true` if the values form a palindrome (read the same forwards and backwards).
L6 Medium
Dynamic Programming (6)
92 93 94 95 96 97
Cheapest Way Up the Stairs
You are on the floor before index `0` of `cost`. From index `i` you may climb 1 or 2 steps into `i + 1` or `i + 2`, paying `cost[i]` when you land. Reach the floor past the last index (the top) at minimum total cost.
L8 Hard
House Robber
Houses in a row hold cash amounts in `nums`. You cannot rob two adjacent houses in the same night. Return the maximum you can collect.
L8 Hard
Climbing Stairs
To reach the top of a stairway with `n` steps you may climb exactly 1 or 2 steps at a time. Return the number of distinct sequences of climbs that reach the top.
L8 Hard
Count Unique Paths
A robot starts at the top-left of an `m` x `n` grid and may only move right or down. Return the number of distinct paths from the top-left corner to the bottom-right corner.
L8 Hard
Coin Change
Given coin denominations `coins` (unlimited supply of each) and an `amount`, return the fewest number of coins that add up to exactly `amount`, or `-1` if it is impossible.
L9 Hard
Can the Word Be Split?
Given a string `s` and a dictionary of words `wordDict` (reusable any number of times), return `true` if `s` can be split into a sequence of dictionary words.
L9 Hard
Trees (7)
84 85 86 87 88 89 91
Invert Binary Tree
Given the root of a binary tree, mirror it vertically by swapping the left and right child of every node. Return the root of the inverted tree.
L7 Medium
Maximum Depth of a Tree
Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root down to the farthest leaf.
L7 Medium
Are Two Trees Identical?
Given the roots of two binary trees `p` and `q`, return `true` if they are structurally identical — same shape, same values at every corresponding position.
L7 Medium
Balanced Binary Tree
Given the root of a binary tree, return `true` if, for every node, the heights of its two children differ by at most 1.
L7 Medium
Symmetric Tree
Given the root of a binary tree, return `true` if it is a mirror of itself (symmetric around its center).
L7 Medium
Root-to-Leaf Path Sum
Given the root of a binary tree and a `targetSum`, return `true` if there is a root-to-leaf path whose values add up to `targetSum`. A leaf is a node with no children.
L7 Medium
Level Order Traversal
Given the root of a binary tree, return the values of its nodes ordered by level, with each level as its own array (root first). Return an empty array for an empty tree.
L8 Hard
Strings (10)
10 17 18 19 20 28 30 34 37 42
Word Lengths
Given an array of words, return a new array holding the length of each word in the same order.
L1 Easy
Starts With Capital
Return `true` if the first character of `s` is an uppercase letter. An empty string or a string starting with a non-letter returns `false`.
L1 Easy
Count Vowels
Count how many of the letters `a`, `e`, `i`, `o`, and `u` appear in `s`. Both uppercase and lowercase count.
L1 Easy
Count Spaces
Return the number of space characters (`" "`) in `s`.
L1 Easy
Capitalize Word
Return `s` with only its first character capitalized. If `s` is empty, return an empty string.
L1 Easy
Common Characters
Return the number of distinct letters that appear in both `s` and `t`. A letter shared just once still counts as one.
L2 Easy
Character Counts
Return a dictionary mapping each character of `s` to the number of times it appears.
L2 Easy
Missing Letter
The string `t` contains every character of `s` plus one extra character somewhere. Return that extra character as a single-character string.
L2 Easy
Palindrome Word
A word is a palindrome when it reads the same forward and backward. Given a single word `word`, return `true` if it is one, ignoring letter case. There are no spaces or punctuation to worry about.
L2 Easy
First Unique Character
Given a string `s`, return the index of the first character that appears exactly once. If every character repeats, return `-1`.
L3 Easy
No challenges found matching your search.