Print a Diamond Pattern
Updated
Problem statement
Given an integer n, return the lines of a filled diamond made of asterisks (*). The top half, including the center row, contains n rows. The bottom half mirrors the top half and excludes a second copy of the center.
There are exactly 2*n - 1 lines. For a zero-based row r, let d = |r - (n - 1)|. The line contains exactly d leading spaces followed by 2*(n - d) - 1 asterisks.
Use ordinary ASCII spaces. Preserve leading spaces, include no trailing spaces, and do not include newline characters inside any returned string.
Function
diamondLines(n: int) → String[]Examples
Example 1
n = 1return = ["*"]The minimum diamond contains one row with one asterisk.
Example 2
n = 3return = [" *"," ***","*****"," ***"," *"]The asterisk counts are 1,3,5,3,1, with 2,1,0,1,2 leading spaces. The center appears once.
Constraints
1 <= n <= 30.
More Apple Phone Screen questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Minimum Difference Between Two Clock TimesMicron Technology · PHONE SCREEN
- Decimal Digits in OrderZoomInfo · PHONE SCREEN
- Add Two Integers Given as StringsJane Street · PHONE SCREENJane Street interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
- Integer to String Without Built-insHudson River Trading · PHONE SCREENHudson River Trading interview questions