Tricky Square Coding Interview Question - JavaScript

preview_player
Показать описание
Solution to a very popular coding interview question asked recently by Square - Encode and Decode String.

#softwareengineering #javascript #shorts

Background Music:
Creative Commons / Attribution 4.0 International (CC BY 4.0)
Рекомендации по теме
Комментарии
Автор

If one of your strings contains the delimiter, your code fails

just use json, or create your similar json parser, Or a uuid so that it never clashes with anything .

better approach would be to have a way to escape the delimiter,
like if my delimiter is $ and value is richA$s.
then the value should be sent and richA$$s
or richA-$s

logic is, if it starts with escape character, its not a delimiter.

rajveersingh
Автор

This shouldn’t pass an interview just because you use var

BookOfSaints
Автор

I would base64 encode the individual strings before joining them, to ensure the delimeter can't exist in the strings

mitchelline
Автор

since this is node, a much much much safer way to do this is with JSON.parse and JSON.stringify, its generally safer. I don't think I've seen a single one of your videos where you do something optimally, I wonder if this code is AI generated or something because as someone who has used javascript and typescript quite a lot, I definitely wouldn't do it this way.

amelted
Автор

I quite like the netstring serialization spec, it's just a data length in a ascii number followed by a colon and the data, example `5:hello`. Redis serialization protocol is quite similar except it also types the data.

ArtiZirk
Автор

If one of your strings contains the delimiter, your code fails

gladepixie
Автор

For times when the delimiter appears in the strong you’re encoding:

Could you just replace all instances of the delimiter with the delimiter twice?

Then when you split the string, the double delimiter *should* produce an empty string in your resulting array which can be interpreted as the literal delimiter .

I think that works for multiple instances of the delimiter in a row in each string as well

alexanderlay-calvert
Автор

Using var will immediately get you rejected

antifa_communist
Автор

I don't get why nobody uses the non-printable ASCII characters - like the "record separator" \30, which is literally designed for this exact purpose...

irnehhenri
Автор

This is a quick and easy solution that works if you know your input range well, but as many have pointed out, your code fails if either of the strings contains the delimiter. To fix this, just use the length of the strings instead. Then just always read the specified number of characters. For example, encode("bloom", "bit") -> "5bloom3bit". You may even save a tiny bit by skipping the last word, as the end of the string serves as a natural terminator anyway. So encode("like", "and", "subscribe") -> "4like3andsubscribe".

bloom
Автор

Use of let instead of const 👎,
unnecessary variable usage, just return the encoded result directly.

karlkrasnowsky
Автор

Ok, I can get a job at square right now

yut
Автор

I'd rather use the null byte \x00 as the delimiter, much much safer.

unclesam
Автор

You then need to call join after split, because it will return an array 😅

nikachkhobadze
Автор

How do You know that this string is not a substring?

Maxiker
Автор

See... I'm just dumb. Encode everything base64 and call it a day.

tisaconundrum
Автор

I would shift the the char codes by an offset K known only by the sender and the decoder. It would still be simple but at least it's actually encoded :)

skylerdjy
Автор

What is this? An interview for kindergarden?

zeroregretsgiven
Автор

1) Don't use VAR. 2) Don't use function syntax. Come on bruh wtf.

YeetYeetYe
Автор

const esc = '/';
const literal = 's';
const delim = 'n';
const encode = strs => strs.map(str => str.replace(esc, esc + literal)).join(esc + delim);
cons decode = str => str.split(esc + delim).map(str => str.replace(esc + literal, esc));

This code converts all / into /s, meaning any / followed by any character other than s, is a special character. We can then special case /n for splitting.
You can never have an accidental /n or /s in a single line, because it will be encoded as /sn or /ss, which are unambiguously the literal slash and then a literal letter.

This only requires that literal and delim don't end with esc, so we can't use for example esc="hello" and literal="hihello". We also can't use C's double backslash convention for the same reason. There may be more requirements, but I haven't figured them out. All I care about is that /, s, n, work.

That said, this means /n is guaranteed to be the special separation delimiter. Once split, we decode each individual line by a map and replace. It has to be in that order.

raffimolero
join shbcf.ru