Giter Club home page Giter Club logo

Comments (10)

kdn251 avatar kdn251 commented on May 8, 2024 1

Hi sorry for the late response guys...
By access, I'm assuming that you're using a heap correctly. Therefore, accessing the min or max value will be O(1) due to the properties of a heap. Search should be lg(n) as you can discard half of each subtree as you traverse the heap again due to its invariants.

from interviews.

changyujiang avatar changyujiang commented on May 8, 2024

If access by index, it can be done within O(1).

from interviews.

yangshun avatar yangshun commented on May 8, 2024

What does access mean actually? I take it to mean accessing a particular value, like in a BST example, accessing a value is O(lgn). To access a particular value in a Heap you would have to first search for it, which is O(n) for non-max/min elements.

from interviews.

yangshun avatar yangshun commented on May 8, 2024

@kdn251 Thanks for your response! My reply as follows:

By access, I'm assuming that you're using a heap correctly. Therefore, accessing the min or max value will be O(1) due to the properties of a heap.

Sure, then I think it should be called Access Max/Min just like in Remove Max/Min. And Access Max/Min should be O(1).

Search should be lg(n) as you can discard half of each subtree as you traverse the heap again due to its invariants.

What are we searching for here? The only time a "traversal" sort of operation is in bubbling up after an insertion and percolating down after a deletion of the root. Search doesn't really apply to heaps.

Hence these are my proposals:

  • Change "Access" to "Access Max/Min" and its time complexity O(1)
  • Remove "Search"
  • Remove "Remove"
  • Change "Remove Max/Min" time complexity to O(log(n))

Would be happy to create a PR for the updated proposal!

from interviews.

changyujiang avatar changyujiang commented on May 8, 2024

Thanks for your reply. Heap is not completed ordered, though. When search for a item, we cannot simply discard half of it but traverse all of items, which is O(n).

from interviews.

yangshun avatar yangshun commented on May 8, 2024

Clearly the O(1) refers to getting the top element of either a max or min heap respectively, not an arbitrary element

Sure, but in the README, the access/search for a heap is written as O(lg(n)) not O(1).

because heaps serve a specific purpose.... Learn your data structures.

I think the point here is to let people revise/learn the data structures through this repository.

from interviews.

EugenHotaj avatar EugenHotaj commented on May 8, 2024

Sure, but in the README, the access/search for a heap is written as O(lg(n)) not O(1).

Ah you're right, completely misunderstood you. Sorry about that. Reopening for visibility.

from interviews.

yangshun avatar yangshun commented on May 8, 2024

Would you like me to submit a PR to address this? Would be glad to do so (:

from interviews.

EugenHotaj avatar EugenHotaj commented on May 8, 2024

Sure, but @kdn251 will have to approve it.

from interviews.

kdn251 avatar kdn251 commented on May 8, 2024

Thanks @EugenHotaj and I see what you mean @yangshun I'll merge your PR now. Thanks for the contribution!

from interviews.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.