⚡ TL;DR
10 intermediate pattern programs in Swift — spiral matrix, hollow diamond, letter triangle, Fibonacci triangle, plus sign, and more. Code and output.
A follow-up to our creative Dart patterns and Go patterns posts, here are 10 pattern programs in Swift. Each one builds interview muscle while staying readable.
1. Spiral Matrix
Fill an n x n matrix with numbers in spiral order.
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7import Foundation
func spiralMatrix(_ n: Int) {
var m = Array(repeating: Array(repeating: 0, count: n), count: n)
var num = 1
var top = 0, bottom = n - 1, left = 0, right = n - 1
while top <= bottom && left <= right {
for i in left...right { m[top][i] = num; num += 1 }
top += 1
for i in top...bottom { m[i][right] = num; num += 1 }
right -= 1
for i in stride(from: right, through: left, by: -1) { m[bottom][i] = num; num += 1 }
bottom -= 1
for i in stride(from: bottom, through: top, by: -1) { m[i][left] = num; num += 1 }
left += 1
}
for row in m {
print(row.map { String(format: "%2d", $0) }.joined(separator: " "))
}
}
spiralMatrix(4)Output
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 72. Hollow Diamond
Diamond outline, hollow inside.
*
* *
* *
* *
*func hollowDiamond(_ n: Int) {
func row(_ i: Int) {
let prefix = String(repeating: " ", count: n - i)
let core: String
if i == 1 {
core = "*"
} else {
core = "*" + String(repeating: " ", count: 2 * i - 3) + "*"
}
print(prefix + core)
}
for i in 1...n { row(i) }
for i in stride(from: n - 1, through: 1, by: -1) { row(i) }
}
hollowDiamond(3)Output
*
* *
* *
* *
*3. Letter Triangle
Each row adds one more letter from A.
A
A B
A B C
A B C D
A B C D Efunc letterTriangle(_ n: Int) {
for i in 0..<n {
let row = (0...i).map { String(UnicodeScalar(65 + $0)!) }
print(row.joined(separator: " "))
}
}
letterTriangle(5)Output
A
A B
A B C
A B C D
A B C D E4. Number Stair
Row i contains i copies of i.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5func numberStair(_ n: Int) {
for i in 1...n {
print(Array(repeating: i, count: i).map(String.init).joined(separator: " "))
}
}
numberStair(5)Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 55. Fibonacci Triangle
Fibonacci numbers continue across rows.
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377func fibonacciTriangle(_ n: Int) {
var a = 0, b = 1
for i in 1...n {
var row: [String] = []
for _ in 0..<i {
row.append("\(a)")
let next = a + b
a = b
b = next
}
print(row.joined(separator: " "))
}
}
fibonacciTriangle(5)Output
0
1 1
2 3 5
8 13 21 34
55 89 144 233 3776. Binary Triangle
Alternating 1s and 0s, starting with 1.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1func binaryTriangle(_ n: Int) {
for i in 1...n {
let row = (1...i).map { (1 + i + $0) % 2 }
print(row.map(String.init).joined(separator: " "))
}
}
binaryTriangle(5)Output
1
0 1
1 0 1
0 1 0 1
1 0 1 0 17. Plus Sign
A + made of stars.
*
*
*****
*
*func plusSign(_ n: Int) {
let mid = n / 2
for i in 0..<n {
var line = ""
for j in 0..<n {
line += (i == mid || j == mid) ? "*" : " "
}
print(line)
}
}
plusSign(5)Output
*
*
*****
*
*8. Hollow Pyramid
Pyramid with hollow inside.
*
* *
* *
* *
*********func hollowPyramid(_ n: Int) {
for i in 1...n {
var line = String(repeating: " ", count: n - i)
if i == n {
line += String(repeating: "*", count: 2 * n - 1)
} else if i == 1 {
line += "*"
} else {
line += "*" + String(repeating: " ", count: 2 * i - 3) + "*"
}
print(line)
}
}
hollowPyramid(5)Output
*
* *
* *
* *
*********9. X-Shape
Diagonals of stars.
* *
* *
*
* *
* *func xShape(_ n: Int) {
for i in 0..<n {
var line = ""
for j in 0..<n {
line += (i == j || i + j == n - 1) ? "*" : " "
}
print(line)
}
}
xShape(5)Output
* *
* *
*
* *
* *10. Sandglass
Inverted triangle on top, triangle below.
*****
***
*
***
*****func sandglass(_ n: Int) {
for i in stride(from: n, through: 1, by: -1) {
let spaces = String(repeating: " ", count: n - i)
let stars = String(repeating: "*", count: 2 * i - 1)
print(spaces + stars)
}
for i in 2...n {
let spaces = String(repeating: " ", count: n - i)
let stars = String(repeating: "*", count: 2 * i - 1)
print(spaces + stars)
}
}
sandglass(3)Output
*****
***
*
***
*****Conclusion
These 10 Swift patterns bring the count across all three languages (Dart, Go, Swift) to a comparable level. Combined with Python, you now have a solid reference for interviews and everyday coding.
Happy coding!

Written by Mihir Pipermitwala
Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.
More about Mihir →