vsanjay_nitdgp's blog

By vsanjay_nitdgp, history, 9 years ago, In English

hello sir/mam,

yesterday i started solving the following problem using stack..

https://www.hackerrank.com/challenges/largest-rectangle

this is my solution

http://ideone.com/SomZF4

i think my code is entirely correct,,,but dont know where it is going wrong.....answer has to be 9 for the sample input....but i am getting it as 5..

could any on pls say error in my code..

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
  Vote: I like it +6 Vote: I do not like it

On input

6

1 2 3 4 5 0

it is giving 9, so I guess you're just not processing rectangles which can be found on stack in the end of the execution. Adding artificial 0 to the end of the input solves that issue.

Btw I answered you, and I hope that I did it correctly, but treat it as a Christmas gift nobody should have ever given you. Do not ever create posts like that, nobody will debug your code, it is your duty to debug your own code, you will not learn if other people will do it for you. You could always add some debugging info and that should helpful.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have 2,3 codes which are almost 3 months old and I haven't debugged it nor I have found anyway to prove it wrong. Nor I found any test case where my code fails? What do I do then ? :P

    I ain't criticizing but sometimes I am stuck with no way to move!

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +4 Vote: I do not like it

      If you're really stuck then one of possible approaches could be writing bruteforce and testgenerator (in many cases completely random will be sufficient in some you need to fulfill some constraints, but still can rely heavily on randomness in some it is harder, ya know, it depends) and compare their outputs on given cases. If I'm not mistaken that is called stress-testing.

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      The only difficult times are when you can come up with only one solution for a problem (not even some slow ones) and it doesn't work, so you don't know how to check the answers it gives other than by hand.

      In all other cases, which cover like 99% of the times and probably your codes too, you do as Swistakk said — write bruteforce and testgenerator then run them until a faulty testcase is found — debugging with a testcase is very easy. If you fail to find a faulty testcase then perhaps your mistake is in either large testcases that the bruteforce can't solve anyway (check array bounds) or corner cases that are just very very rarely generated by random test generator (think about some specific cases that may be bad for your program).

      I'm personally very used to debugging with generator and slow solution that I do it when on competitions and I do it to be sure of the correctness of my code in case there is no feedback.

      So I'm almost certain that you are not "stuck with no way to move"

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I have been using brute checking since some months now ! And sometimes when solution are public I try running some random code and mine code for a fixed sample space!

        But I do not understnad how to generate test data for some problem which has a graph as an input more badly a tree as an input !

        Most recent being Link How to deal with such situations?

        And @mewat1000 I really do not have anyone to ask. Sometimes friends are helpful , most of the times I debug it myself! But in the problems like above I am stuck!

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          if you fail trying so much, I suggest u forget about that and do some other problems and look into it after 2-3 months.

          one unsolved problem won't affect u that much, but wasting too much time trying will prevent u doing new things

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          Generating random connected graphs or trees is a task by itself, and it's not too difficult.

          The important thing to remember is that you don't need a perfect test generator. I often rely on quantity over quality.

          Having said that, one way to generate tests in the task you mentioned is simple : generate random N,M,K. Choose M different edges with random lengths. Then set Q so that you ask a query about every pair of vertices and set constraints on generating relatively small graphs (10-20) vertices. Then run the generator and some slow but correct solution until they produce different outputs.

          If your mistake can be replicated in a small graph, you'll surely soon find it. Since graphs are small you can probably run thousands of tests per second.