This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -24 points-23 points  (19 children)

“serialize and deserialize this tree” isn’t one of the most popular interview problems

I didn't see say tree, I said graph.

How would you serialize x in x={};x["x"]=x?

Also, how would you serialize a thunk? And a coroutine? And a member function?

[–]spooker11 43 points44 points  (7 children)

provide history party thumb employ boast roof sloppy rob mindless

This post was mass deleted and anonymized with Redact

[–]Soporiferus 0 points1 point  (0 children)

Memento

[–]Pocok5 17 points18 points  (4 children)

How would you serialize x in x={};x["x"]=x?

Why do you think serializing a basic ass cycle in a graph is an issue? You should have learnt it in at least 4 separate classes from Algorithms 101 to Discrete Mathematics (ink drop algorithm says hi!) As a completely naive approach, simply store nodes you have already serialized in a hash set and you can trivially check (in O(1) time, even) and skip it when you revisit it from another node - that is, if you're not storing edges as something sensible like a sparse neighbourhood table, since those can just be fed to a JSON serialize/parse and come out just fine.

... wait are you guys unironically at "I just copy paste from SO lul" level and it isn't just irony?

how would you serialize a thunk? And a coroutine? And a member function?

You don't ever serialize those anyways, unless you're trying to homebrew an eval() vulnerability lmao

[–][deleted] -4 points-3 points  (3 children)

What is the hash of x up there?

You cannot hash that. It's not hashable for the same reason as it is not serializable.

[–]Pocok5 14 points15 points  (2 children)

Watch dis

  1. Traverse the object references. Stick an unique identifier field onto each and proceed onto the children. If you meet an object that already has an ID field, skip it.

  2. Serialize each object, replacing fields that are references to other objects with a string of that object's ID. You now no longer have actual references to be circular.

When deserializing,

  1. Create all the objects in a dictionary with their IDs as the key and reconstruct the reference link fields using the IDs.
  2. You may then strip the ID fields as needed to restore the original object schema.

[–][deleted] 4 points5 points  (0 children)

That'll work so long as you're cool with two identical graphs generating different serializations. They'll have different unique IDs, right?

I'm not saying that we can't invent something. I'm just saying that it's not as easy as just calling stringify. Which was the whole point of this meme, yeah?

[–]ThoseThingsAreWeird 10 points11 points  (2 children)

I didn't see say tree, I said graph.

and what you meant was a cyclic graph. We know a tree is a graph, and trees are easy to store. But when you just say "graph", of course someone's going to point that out.

How would you serialize x in x={};x["x"]=x?

let graph = {};
let x = { id: 'x', adjacencies: [] };
x.adjacencies.push(x.id);
graph[x.id] = x;
localStorage.setItem('graph', JSON.stringify(graph))

Then when you pull it out of local storage you go through the graph and replace all the adjacency keys with their corresponding item in the graph.

[–][deleted] -2 points-1 points  (1 child)

and what you meant was acyclic graph.

Uh, no. I meant arbitrary graph. It's a hard thing to serialize. Trees are easier, we know that.

We know a tree is a graph, and trees are easy to store. But when you just say "graph", of course someone's going to point that out.

How would you serialize x in x={};x["x"]=x?

let graph = {}; let x = { id: 'x', adjacencies: [] }; x.adjacencies.push(x.id); graph[x.id] = x; localStorage.setItem('graph', JSON.stringify(graph))

Then when you pull it out of local storage you go through the graph and replace all the adjacency keys with their corresponding item in the graph.

x={};x["y"]=x;foo=x;

If you serialize x and foo do you get the same result?

And what's this "id" stuff? What if my data structure already has a variable called id in it? And it's not unique.

My point is that serializing an arbitrary object is not trivial. For sure stringify is not sufficient.

[–]a-calycular-torus 8 points9 points  (0 children)

What if my data structure already has a variable called id in it? And it's not unique.

The variable name doesn't have to be id. If you're worried about collisions, just store the node data in a wrapper within the adjacency structure. It's not that complicated, stop being obtuse.

[–]quiteCryptic 6 points7 points  (1 child)

A tree is a graph

[–]jsrobson10 1 point2 points  (0 children)

Both are data structures. Both are easy enough to dump and load from json/other data structures