The Workspace

Swift DSA Lab

Master algorithms & data structures using idiomatic Swift. Practice challenges from Arrays to Dynamic Programming in a clean editor.

Arrays & Hashing (35)

01
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
02
Count Positive Numbers
Count how many integers in `nums` are strictly greater than `0`. A number like `0` itself does not count.
L1 Easy
03
Largest Element
Return the single largest integer in `nums`. You may assume the array always has at least one element.
L1 Easy
04
Smallest Element
Return the single smallest integer in `nums`. You may assume the array always has at least one element.
L1 Easy
05
Count Target
Count how many times `target` appears inside `nums`, then return that count.
L1 Easy
06
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
07
Last Element
Return the last integer in `nums`. If the array is empty, return `-1`.
L1 Easy
08
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
09
Digit Sum
For a non-negative integer `n`, return the sum of its decimal digits. For `0`, return `0`.
L1 Easy
11
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
12
Temperature Filter
You recorded the temperature each day in `temps`. Return how many readings are strictly greater than `threshold`.
L1 Easy
13
Array Average
Return the average of the integers in `nums` as a `Double`. If the array is empty, return `0`.
L1 Easy
14
Contains Value
Return `true` if `value` appears anywhere in `nums`, and `false` otherwise.
L1 Easy
15
Count Unique
Return how many distinct integers appear in `nums`. Duplicates should be counted only once.
L1 Easy
16
Combine Arrays
Return a single array formed by all of `a` followed by all of `b`.
L1 Easy
21
Second Largest
Return the second largest *distinct* value in `nums`. If the array has fewer than two distinct values, return `nil`.
L2 Easy
22
Reverse Array
Return a new array containing the values of `nums` in reverse order. The original array must not be modified.
L2 Easy
23
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
24
Count Occurs Once
Return how many distinct values in `nums` appear exactly once. Values that repeat do not count at all.
L2 Easy
25
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
26
Intersection Count
Return how many distinct integers appear in both `a` and `b`. A shared value is counted once, even if repeated.
L2 Easy
27
First Repeated Value
Return the first value in `nums` that appears more than once. If every value is unique, return `nil`.
L2 Easy
29
Frequencies
Return a dictionary that maps each distinct integer in `nums` to the number of times it appears.
L2 Easy
31
Majority Element
Return the integer that appears more than `nums.count / 2` times. If no value is that frequent, return `-1`.
L2 Easy
32
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
33
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
35
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
36
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
38
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
39
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
40
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
41
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
59
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
90
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
98
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
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
44
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
45
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
46
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
47
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
48
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
49
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
50
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
51
Palindromic Number
Given an integer `x`, return `true` if it reads the same forwards and backwards (ignoring the sign), otherwise `false`.
L3 Easy
99
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

Stack (8)

60
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
61
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
62
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
63
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
64
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
65
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
66
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
67
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
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
69
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
70
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
71
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
72
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
73
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
74
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
75
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
100
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
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
77
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
78
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
79
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
80
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
81
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
82
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
83
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