Mask a card number
Support staff can see the last four digits of a card and nothing else.
- Every character except the last four becomes an asterisk.
- Anything four characters or shorter comes back untouched — there is nothing safe to hide.
maskCard(cardNo: string) → string
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func maskCard(cardNo string) string {
}
Worked examples
| Call | Result |
|---|---|
maskCard("4506349012345678") | "************5678" |
maskCard("12345") | "*2345" |
maskCard("1234") | "1234" |
maskCard("") | "" |
Hint
Build the asterisks from the length, then glue on the tail.
Reference solution in Go
func maskCard(cardNo string) string {
if len(cardNo) <= 4 {
return cardNo
}
return strings.Repeat("*", len(cardNo)-4) + cardNo[len(cardNo)-4:]
}