We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Interview Preparation Kits
- Lego Blocks
- Discussions
Lego Blocks
Lego Blocks
Sort by
recency
|
86 Discussions
|
Please Login in order to post a comment
I was so ready to give up on this Even with @dusanki solution, I didn't understand why the maths works.
It was only when I found on the internet an explanation with pictures that it all clicked.
I will probably implement it now. Just for closure. But I feel like Hackerrank has stolen a part of my life 😛
And now I've finally implemented an accepted solution I offer a little follow up to @jltd0jsn5's hint.
Unfortunately BigInt doesn't (by itself) save the day for Javascript developers as it's too slow.
I say that but I see solutions below whch seem to rely on it. So feel free to ignore me it if works for you.
so what kind of education do you need to be able solve this? i've been in IT 18 years and i don't think i could've ever solved this without internet assistance
This is a pretty intense combinatorial problem. For the record just figuring out all of the ways to stack n number of 1x2 lego bricks contiguously is itself a deep theoretical math question for mathemeticians, and only has a theorem that was proven in 1988.
Even attempting to brute force the solution is itself a challenge to write, let alone something reasonably efficient. I think if any company expected you to do this problem I would ask if any of their engineers could solve it provided you make a small tweak to the parameters.
I don't know who needs to hears this, but as a 8 YoE software engineer, this took me an entire weekend to solve ^^'.
A little spoiler-free tip I wish I had:
The very idea anyone would think this is a good thing to test someone on in a time limited scenario is patently absurd, how is this how hiring is done now?
Having some difficult with a valid JavaScript version. The values that are failing are when combinations get over MOD (10^9 +7) Has anybody got a JavaScript version working or can spot the error in this code? Thanks
*(legoBlocks(8,10) on my code is giving 634608018.
Hacker rank is giving 634597424*
}
This question wrinkled my brain until I looked at Willis42's code. Here's some extra annotation in case it helps.
def legoBlocks(n, m): # the ways to build a line of m from 0 up to 4 using legobricks from 1-4 # this takes the prev sums and adds one more way to it each time permutations = [0, 1, 2, 4, 8] mod = 10**9+7 # just keeps our numbers from getting too large - wraps if so