FAANG Interview Question : Arrangement of Bricks
Hello reader, this is my first post on Medium. I hope you enjoy it!
I was asked the below question in one of the coding interviews, here is my approach and solution for the same.
Question:
We are given a wall of height h and length l and we have to design it with bricks in such a way that:
- When the bricks are stacked on top of each other no two bricks’ edges coincide(except at the corners).
- After stacking the bricks the total length is l and height is h.
- There are 2 kinds of bricks: 2X1 and 3X1.
Example:
Wall 1 = length:7 and height:5
Wall 2 = length:6 and height:4
Observations:
- The arrangements can be alternated. So we only need 2 sets of combinations whose edges do not coincide.
Solution:
- For odd length:
- one combination can be made by having the first brick as 3X1 and subsequent bricks as 2X1.
- the second combination can be the reverse of the first combination(2X1 bricks followed by one 3X1 brick).
2. For even length:
- one combination can be made by filling with all the 2X1 bricks
- the other combination can be made by having the first brick as 3X1 followed by 2X1 bricks and followed by one last brick of 3X1.
Analysis of Solution:
- For odd length:
- Total number of 3X1 bricks used= 1
- Total number of 2X1 bricks used = (l-3)/2
- Total bricks in the first and second combinations are the same.
2. For even length:
- Total number of 3X1 bricks in the first combination=0
- Total number of 2X1 bricks in the first combination = l/2
- Total number of bricks in the first combination=l/2
- Total number of 3X1 bricks in the second combination=2
- Total number of 2X1 bricks in the second combination = (l-6)/2
- Total number of bricks in the second combination=2+((l-6)/2)
Edge Cases:
Some of the combinations are not achievable for this kind of arrangement:
- if the length is 0.
- if the length is 1.
- if the length is 4.
Code
Thanks for Reading!
Discord link: https://discord.gg/zEHhKWQm for any questions, debugging sessions.