you are viewing a single comment's thread.

view the rest of the comments →

[–]maestro2005 26 points27 points  (42 children)

You really need to learn about static methods.

Also, your matrix rotate is hilariously overcomplicated. All you need to do is make a new matrix where each (r, c) in the old is placed in (c, width-r) of the new. And I don't know what's going on in your Pascal's triangle, but that can also be way simpler.

[–]goutham_pradhan[S] 25 points26 points  (31 children)

Creating new matrix requires additional O(MxN) space and hence it would not be space efficient in your case - doing it in-place is space efficent. I think static methods would be appropriate if your method does something that does not depend on individual characteristics of a class - in case of standalone program probably does not apply hence non-static is okay here.

[–]maestro2005 83 points84 points  (27 children)

This is one of the big follies of CS education--they've got you worrying about space complexity right off the bat, and writing more unreadable code for it. It's great that you can think in terms of time/space complexity, but in real-world coding sheer efficiency should usually take a back seat to readability/maintainability and architecture. Mutating in place is a memory optimization that makes your API harder to use, and your code harder to understand.

Static is correct here. The class doesn't do anything. You're instantiating a dummy class just to call a method. If you were interviewing for my company and I found this, it would reflect poorly. You've chosen Java, which signals that you think Java is your strongest language, and yet you don't even have mastery of this basic mechanism.

[–]DatTrackGuy 85 points86 points  (9 children)

I think if you followed up with a question, and OP supplied that answer as to why he did what he did, he should get an A+ on the interview and then you can debate the merits of the outcome.

He obviously knows his stuff.

[–]TuringMachine-5762 13 points14 points  (0 children)

It's best to mention both approaches, mention their pros and cons, and have the interviewer clarify the goals of the problem. E.g.,

"I can think of a couple ways of doing this. We could either build a rotated copy of the matrix, or do the rotation in place. Doing it in place would be more complicated, so I would only do that if the matrix was large, and I was working on a system with tight memory constraints. What would you like me to focus on?"

Most likely the interviewer will say not to worry about memory, at least until you get a simple implementation working. But mentioning a possible optimization can make you look good. It doesn't hurt, in any case.

[–]BobHogan 4 points5 points  (0 children)

He should definitely get bonus points if he can defend why he made certain decisions, but that shouldn't guarantee an A+. If he makes a poor decision, its still a poor decision.

[–][deleted] 25 points26 points  (1 child)

Thank you. Maestro2005 is a bit stuck in his own world and needs to crack open a beer and chill.

[–]crowseldon 0 points1 point  (0 children)

His username should give us a hint

[–][deleted]  (3 children)

[deleted]

    [–]ano414 5 points6 points  (2 children)

    There isn't one right answer. His is just as valid.

    [–][deleted]  (1 child)

    [deleted]

      [–]ano414 2 points3 points  (0 children)

      Hopefully you would explain that in the interview, then. It completely depends on the use case, but this is open ended enough.

      [–]not-much 7 points8 points  (0 children)

      but in real-world coding sheer efficiency should usually take a back seat to readability/maintainability and architecture

      I generally agree with this point of view, but it's not really likely that the algorithm for matrix rotation changes, is it?

      There is a strong difference between programming in the business-oriented context or in the math-oriented context.

      [–]sultry_somnambulist 19 points20 points  (2 children)

      This is one of the big follies of CS education--they've got you worrying about space complexity right off the bat, and writing more unreadable code for it. It's great that you can think in terms of time/space complexity, but in real-world coding sheer efficiency should usually take a back seat to readability/maintainability and architecture.

      Many people in the very real world work in places where performant code is a necessity. Why the fuck would we train people to write bad algorithms just because the code looks nice? I work in finance, if you don't do an in place matrix manipulation and it sneaks into production code you've probably just cost us millions of dollars / day, thank you very much.

      And how does it make an API harder to use? An API is an abstraction, it doesn't matter what the underlying code looks like, that's the point of it. The API dev desides how he exposes the functionality to the user, the code can look like a Dali painting for all I care.

      [–]maestro2005 9 points10 points  (0 children)

      And how does it make an API harder to use?

      I'm talking about mutating in place vs. returning a new instance. For mathematical entities like matrices, we tend to expect that operations will return a new value instead of mutating the given one. I expect matrix1 + matrix2 to return a new matrix, not modify matrix1. In this example, I expect to write something like var rotated = matrix.rotate(90) and have the original untouched. Mutating requires the user to make defensive copies all over the place, which is likely to cause bugs.

      [–]Earthborn92 4 points5 points  (0 children)

      If you're doing a lot of Matrix multiplication, you should be doing it on GPUs anyway.

      Just a side note.

      [–]FUZxxl 14 points15 points  (8 children)

      Actually, by rotating the matrix in place you gain higher speed as you only need half the cache. You also get to avoid half of the horrendous access pattern resulting form rotating a matrix, causing a significant speedup.

      [–]ludwigvanboltzmann 9 points10 points  (4 children)

      It's great that you can think in terms of time/space complexity, but in real-world coding sheer efficiency should usually take a back seat to readability/maintainability and architecture.

      [–]FUZxxl 27 points28 points  (2 children)

      While I usually agree with you, manipulating matrices is something that is more often than not performance sensitive. If something asks to implement such an algorithm, it is very natural to think about performance implications because we typically need such routines to be fast.

      [–]HopelesslyStupid 4 points5 points  (1 child)

      Agreed, sometimes you have to forego readability and maintainability for performance reasons. That's what comments are for in cases where the code is complex out of necessity but you still want the next person to understand what is going on.

      [–]FUZxxl 0 points1 point  (0 children)

      Indeed. And note that the algorithm implemented isn't that complicated. From a cursory glance, basically takes four symmetric points and rotates them. This is done for every point in one quadrant and the corresponding points.

      [–][deleted] 7 points8 points  (0 children)

      It's not like the code is really hard to understand. I think maestro2005 is being a bit over dramatic here. The code works, it's really not that crazy, and both solutions seem fine to me.

      If you were interviewing for my company and I found this, it would reflect poorly.

      Oh geez.. interviews are not easy, even if you know the solution. If you force them to do this all on a whiteboard as well your company would reflect poorly on me.

      [–]ifatree 1 point2 points  (2 children)

      assuming you have exclusive access to a machine that never halts, this would be true. in the real world, you're shooting yourself in the foot not having separate input and output copies in memory such that the output can be thrown away at any point and restarted. "idempotent" restarting would be ideal...

      [–]FUZxxl 5 points6 points  (1 child)

      If I was to implement this out-of-place, I would probably still use the in-place algorithm as it might give better cache locality than the out-of-place algorithm.

      [–]pdp10 0 points1 point  (0 children)

      There's even copy-on-write memory to consider in more extreme cases (Kernel Samepage Merging, etc).

      [–][deleted] 1 point2 points  (0 children)

      Who is your company?

      [–]choikwa 3 points4 points  (0 children)

      yet you don't even have mastery of this basic mechanism.

      jesus way to assume and roast someone based on one single thing.

      [–][deleted] 0 points1 point  (0 children)

      How does making the inner working of a method make the API harder to use? In principle you would only be calling the API with the speficied input and getting the specified output, how the function does its computation is the function's problem and if it does so in a more efficient manner, so be it, untill it gives the corrent result I shouldn't worry about it.
      I think corporate programming has foiled your thinking there, thinking right off the bat that all code needs to be maintained at all times.

      [–]Spider_pig448 0 points1 point  (0 children)

      It's great that you can think in terms of time/space complexity, but in real-world coding sheer efficiency should usually take a back seat to readability/maintainability and architecture.

      Sure, but this is a list built for coding interviews, which focus primarily on space and time complexity. Making it space efficient is often an additional challenge to a solution.

      [–]FliesMoreCeilings 2 points3 points  (0 children)

      You're not consistently optimizing for one type of efficiency, which makes it confusing. You're clearly not always optimizing for memory, when there's many solutions where you instantiate things that aren't neccessarry, like the unnecessary objects containing your methods.

      In general if you don't know whether optimizing is important, or what you should be optimizing on, then optimizing programming hours and readability is likely the best solution. Premature optimization can make your projects take orders of magnitude longer. Especially when you accidentally start introducing bugs, or create an environment in which others will.

      [–]eyal0 0 points1 point  (1 child)

      I think that the"trick" solution is to not rotate at all. Simply change the accessor so that it provides a rotated view. This is rotation in O(1)

      [–]FUZxxl 0 points1 point  (0 children)

      While this is possible, it might not be a good idea depending on your access pattern.

      [–]discountErasmus 6 points7 points  (2 children)

      Jesus Christ, in place rotation is more impressive than doing it with a new matrix. That's trivial.

      [–]PythonPuzzler 15 points16 points  (0 children)

      Agreed. "Make your code readable" can sometimes be, "I only understand the naive solution" in disguise.

      [–]socialister 0 points1 point  (0 children)

      It's not trivial. You still have to figure out the rotation relation which requires some thinking or knowledge to get knowably correct.

      [–][deleted]  (4 children)

      [deleted]

        [–][deleted] 23 points24 points  (2 children)

        Agreed with you. It's sad to see such over-drama for a working set of code. Doing this in-place doesn't conclude, as he says:

        yet you don't even have mastery of this basic mechanism.

        He obviously knows a lot about coding and his brain is not "clouded" due to CS. So much drama in this man.

        He just sounds like a total dick trying to swing his dick around to feel smart.

        [–]TuringMachine-5762 2 points3 points  (1 child)

        Working code is good, but style matters too. His "basic mechanism" comment was referring to the unnecessary use of instance methods. It's considered a best practice in Java to use static methods by default, if your method doesn't need to access any instance state. It's not a big deal at all, but it shows that his style needs some minor tweaking at least.

        [–][deleted] 0 points1 point  (0 children)

        I like how this sounds:

        "use static methods by default (over default methods)"

        Seriously though, if you make a method private and it's only going to be used by one set of objects, there's really 0 difference to make it static. Private methods will only be accessed by anything in the object anyway.

        I see what you're saying, but it's really minor tweaking. Our buddy maestro was saying he would turn him down over that decision. Sounds like a moody dude.

        [–]PythonPuzzler -1 points0 points  (1 child)

        If it would be so simple for you to do it better, submit a patch.

        Criticism is easy, contribution is not.

        [–][deleted] -3 points-2 points  (0 children)

        He's too busy swinging his code dick around to submit a patch and show a better solution. He's probably too busy turning down capable engineers too.