r/compsci • • May 17 '19

20+ String Coding Interview Questions for Programmers

https://hackernoon.com/20-string-coding-interview-questions-for-programmers-6b6735b6d31c
113 Upvotes

41 comments sorted by

View all comments

19

u/icendoan May 17 '19

strings are an array of characters

Not in utf8, they're not. Ascii is not the be all and end all.

5

u/quentech May 17 '19

What runtimes store strings in memory as utf8?

3

u/czdl May 17 '19 edited May 17 '19

Cocoa / OSX

Windows uses UTF16 which has exactly the same issue.

1

u/Nerdlinger May 17 '19

I believe Objective C/NSString still uses UTF-16 for the backing store, though Swift recently switched to UTF-8.

1

u/czdl May 18 '19

Perhaps on iOS, but querying NSString for its representation, or pulling bytes out on OSX reveals UTF-8.

0

u/quentech May 17 '19

has exactly the same issue

Windows uses fixed 16-bit width characters and string are in fact arrays of them.

1

u/nzodd May 18 '19

What do you mean, the win32 api? So how does it store characters outside the BMP then ?

1

u/masklinn May 18 '19

Same as java or JavaScript, surrogates are visible and you can easily get or create outright invalid text, even ignoring the grapheme cluster issue.

0

u/czdl May 18 '19

They’re arrays of them in the same sense that utf8 is an array of characters. If you don’t handle surrogates, you break the string.