35
donuts
7y

Wooh! I implemented a heap.... that only took... 5 hours...

I am so screwed for my interview next week..... :(

Comments
  • 1
    I'm no java guy, why you need to implement a heap doesn't java have pointers ?
  • 3
    Slight recommendation: use 'b.compareTo(a)' instead of multiplying by -1?
  • 0
    @linux-colonel I wanted to flip the order. Need to define the comparator for the heap which is generic, can compare any type.
  • 0
    @Qchmqs yes but as you probably can tell from the project tree, I'm studying data structures... And Algorithms
  • 1
    @allanx2000
    b.compareTo(a) would have the opposite order to a.compareTo(b) 😊
  • 0
    @linux-colonel but then I would have to rewrite the whole thing... -1* is much easier
  • 0
    @linux-colonel maybe not rewrite but this is faster and easier :)
  • 1
    @allanx2000
    You wouldn't have to rewrite the whole thing, that lambda would just be (a, b) -> b.compareTo(a). I guarantee you it'd look better in an interview situation. Readability is everything!
  • 1
    @linux-colonel ah OK, yea I would... Just not after 5hrs coding
  • 1
    Btw is using an ArrayList as the underlying data structure correct?
  • 0
    at least intellij :)
  • 0
    Out of curiosity what is the constructor doing with the value of the compareTo value? I would assume it would have something to do with minimum heap size, but since it could be -1 passed in, I'm just curious...
  • 0
    Btw one recommendation if this is code you are submitting for the interview- clean up the commented out import statements and add more javadoc and comments. Documentation and clean code is really, really important. Especially in an interview where they usually judge code style pretty closely.
  • 0
    @Yankeesrule

    It's an implementation of Comparator, which the heap uses to sort the values
  • 1
    @Yankeesrule this is just practice, finally I understand how to implement a heap
  • 2
    @allanx2000 ah, got it. I read the code quickly and didn't see that it was a bi-function. Java 8 lambdas are awesome but definitely need to pay attention when reading it. My bad.

    Congrats for figuring it out! It doesn't matter how long it takes. What matters is you got it. To be quite honest in a real world scenario I'm sure there's a heap library you could just import. But you taking the time to understand it is huge. So congrats!
  • 0
    @Yankeesrule didn't know I could do this till I saw a lightbulb that said it could use a lambda
  • 1
    @allanx2000 yeah. Prior to java 8 you coulda used an anonymous inner class that implemented comparator but this is WAY cleaner. Understanding lambdas is a big achievement too since they are kind of mind exploding at first (though streams are stranger at first). So kudos. I think you are better off than you think.
  • 1
    @Yankeesrule, lambdas make the code so nice to read. As an Android developer I can use the RetroLambda library which is not complete but it's nice too.
  • 1
    @Eariel I know! When I first read about them I was very dubious about how complex it would make the code but if anything it's done the opposite. They are great.

    ... But you definitely need to be careful not to abuse them. Like I've had to stop myself from writing a function or supplier within a function just because then it can be difficult to understand the business logic when reading it at first.

    It's strange though because I really do love them but then I show the code to one co-worker who can't stand them- to her the business logic is not "linear" and so it's not readable for her. I just think to myself she is lucky she is working in Java because she would be so screwed dealing with pointers in other languages
  • 0
    @allanx2000 when you are building everything up from scratch, I don't think it makes sense to use an ArrayList, use a resizing array instead.
  • 0
    Doesn't Java already has a "PriorityQueue", which works as a heap?
  • 0
    Journey Moon?
  • 1
    @pascalwacker yes but uhm for interview u scrutiny have to know how these work
  • 0
    @sergioct hacker rank problem
  • 0
    @ravik yes but uhm thats exactly what an array list is though. Just track the size and when it hits double it
  • 0
    @Yankeesrule well actually my language of choice is C#, Lots lambdas
  • 0
    Out of curiosity, is this really useful? I had a class for a whole semester about "Information Structures" where we talked about LinkedList, DoublyLinkedLists, trees, heaps, graphs, etc. We did a cool little project during the semestre (the really simplified model of a social network) but it was mostly just to apply the algorithms and classes from the lectures so I didn't get the point of it.
  • 1
    @milkbytes well i was just following this book http://goodreads.com/book/show/...
  • 0
    @allanx2000 I have that book, I'm planning to start a "training regime" later this year since I'm going to need to apply for internships for my degree. Still, these are the kind of concepts that are interesting and seem really cool but I don't see them being actually useful. I mean, at least creating a heap logic by yourself when there are tons and tons of implementations already available. IDK.
  • 0
    @milkbytes yep that's what I thought too. I'm self taught, didn't study CS but all the good tech companies ask this stuff and I just have to bite the bullet...
  • 0
    @allanx2000 My field of studies is actually Software Engineering and I'm yet to understand the usefulness of doing my own implementation of heaps and etc. Maybe it's one of those things that I'm only going to understand when I need it 😂
  • 0
    @Yankeesrule if you are familiar with RX(Java, JavaScript, RX Android), doesn't matter, those emitted events are just streams, basically. You can change and manipulate data as it passed through the stream and them collect it or just pass it to a function.
Add Comment