Live data from Hacker News

Algorithms and Data Structures Explained and Implemented in JavaScript

github.com

31–40 of 45 posts

Re: Algorithms and Data Structures Explained and Implemented in JavaScript

#32
post #27

Earlier quoted context omitted.

Wait are linked lists not better for first-in first-out data structures? I thought arrays were not preferred in those cases because shift/unshift are highly inefficient. Am I wrong? I thought array-based queues resulted in O(n) queue/dequeue. Now for stacks, which are last-in first-out, it makes sense to use an array.

> I thought array-based queues resulted in O(n) queue/dequeue. Not necessarily. A circular buffer[1] can be used as a queue with O(1) queue/dequeue. C++ implementations (gcc?), IIRC, uses an interesting array-of-arrays approach; it also has O(1) queue/dequeue. I'm not sure why the array-of-arrays approach is better than a circular buffer, though. Array based designs can result is less allocations, and maybe less over…

Resizing a fully contiguous circle buffer would cause a copy every element as well forcing you to make a single contiguous memory section. Array of arrays just needs to resize the top level array.

Re: Algorithms and Data Structures Explained and Implemented in JavaScript

#34

is there a similar thing with python too ?

https://www.geeksforgeeks.org

I have mixed feelings about that site.

There are people posting verbatim interview questions, which is a breach of interview NDA.

Re: Algorithms and Data Structures Explained and Implemented in JavaScript

#35
post #27
post #25

The documentation and code quality is all good. The implementation choices leave some things to be desired. The Queue and Stack implementations are Linked Lists instead of array backed, the hash table is closed instead of (the only barely more complicated) open Robin hood hash table scheme, the union-find/disjoint-set implementation doesn't have path compression or rank unions. Overall very good, but it could be Grea…

Wait are linked lists not better for first-in first-out data structures? I thought arrays were not preferred in those cases because shift/unshift are highly inefficient. Am I wrong? I thought array-based queues resulted in O(n) queue/dequeue. Now for stacks, which are last-in first-out, it makes sense to use an array.

Only if you eagerly shift all elements on every operation. If you allocate extra space, and only shift every N operations (N being the max size of the list), it’s O(n/n), eg O(1) amortized. As mentioned already, you can represent it as a circular buffer to save a bit of memory. But this cost analysis also means that a simple array can be much faster than a linked list for almost all operations in theory (excludes random insert given an insert token, which is not an index). I believe there’s also experimental evidence of this showing that an array is almost always preferable to a linked list - even in the cases were a linked list is often taught as the obvious choice (such as a queue). Yep, surprised me quite a bit too.

Re: Algorithms and Data Structures Explained and Implemented in JavaScript

#36
post #32

Earlier quoted context omitted.

> I thought array-based queues resulted in O(n) queue/dequeue. Not necessarily. A circular buffer[1] can be used as a queue with O(1) queue/dequeue. C++ implementations (gcc?), IIRC, uses an interesting array-of-arrays approach; it also has O(1) queue/dequeue. I'm not sure why the array-of-arrays approach is better than a circular buffer, though. Array based designs can result is less allocations, and maybe less over…

Resizing a fully contiguous circle buffer would cause a copy every element as well forcing you to make a single contiguous memory section. Array of arrays just needs to resize the top level array.

But the copy only has to move n items, but was constructed with m items, where n On the other hand, an array of arrays (no recursion) doesn’t change the big-O complexity cost, just the constant multiplier. That should definitely improve performance of the uncommon operation (the copy), but hypothetically might slow down the actual queuing operations (and drastically reduce throughout)

But maybe you meant the array of arrays to be recursive? That seems like it would alter the big-O (from n+m to log(n)+m). But typically m>>n, so the net result is the same.

Re: Algorithms and Data Structures Explained and Implemented in JavaScript

#37

This repo is beautiful. The README is detailed and clear, and the contents seem pretty exhaustive. As a native JS guy I'm especially excited to check out the implementation of non-tree graphs! Having said all that: The algorithms I've had to tackle at work tend to be fairly trivial or total one-offs involving scheduling events in human time. How much algorithmic work do you other JS folks end up doing?

> How much algorithmic work do you other JS folks end up doing? I recently built a questionnaire/form editor/builder which relies heavily on tree structures. I ended up writing a lot of tree walking code. It was the first time I've ever needed to implement interview-like questions in an actual project. I make heavy use of functional programming in Javascript, which lends itself to really clean implementations of algo…

Is it just me or does the hastebin site load almost instantly? On iOS. Was nice.

Re: Algorithms and Data Structures Explained and Implemented in JavaScript

#39

Earlier quoted context omitted.

I have to admit I’m far from a FP guru, very little experience with true functional languages like Haskell etc. For me it’s more a question of style and applying functional paradigms when possible, eg treating functions as first class citizens, making heavy use of .bind() and passing functions as arguments. I use ES6 via Babel and actually find it to be quite expressive and powerful when used correctly. I just try to…

It requires use of Babel, but you might find the function bind operator an interesting alternative to .bind(). https://babeljs.io/docs/plugins/transform-function-bind/

That’s very cool! But I’d be hesitant to introduce something non-standard into the codebase. Sure the toolchain can handle it but I’d be worried other devs won’t immediately recognize the syntax.

Re: Algorithms and Data Structures Explained and Implemented in JavaScript

#40
I would be interested in hearing feedback on this book [1]. I bought it for a friend who's getting started with programming, coming from a different field. It seems a good book overall, but I haven't had the time to take a very deep look at it.

[1] https://www.packtpub.com/web-development/learning-javascript...

Post reply on HN