There are some modifications: 1. We can use map to store ladder and snakes in the form of map[startPosition]=finalPosition. 2. Instead of creating a queue and popping the front and pushing it to the back. Simply keep a fixed array and keep increment the index in a circular manner i.e index=(index+1)%noOfPlayers thus index will give the current player at any time
Could've refactored the if-else cascading at 15:03, the logic looks very reduandant at places. You could've done teh nextposition[0] == nextcell check once after checking for both the ladders and snaked. If (nextposition[0] > nextcell) {// ladder} else {// snake bit}
code may be optimized, instead of creating the list of snake, ladder, and jumper, we can create a map for where the key is start point and value is endpoint, this way we can save time to search if target position is ladder/ snake or not int o(1) time, after searching on the internet, I think i am in right place for low-level designing
You are right, it is just that the list will not be too big in a game like this, and readability wise it felt better hence my choice of list but time complexity wise you are right. Hope content are helpful. Do like and subscribe and share with others 🙂
Timestamps 00:04 - Introduction to low-level design of a customizable Snake and Ladder game. 02:19 - Initialize game configuration for Snake and Ladder. 04:32 - Designed a dice class and a generic jumper class for Snake and Ladder game. 06:53 - Implemented game board features for Snake and Ladder. 09:10 - Implemented game mechanics for starting and managing player turns. 11:28 - Game logic manages player positions and winning conditions in Snake and Ladder. 13:41 - Handling player movement for snakes and ladders in the game. 15:58 - Game logic involves players, dice rolls, and handling snakes and ladders. 18:12 - Overview of Snake and Ladder game board design.
Great Video. Thanks for that. I have a question. In your design, the game board is tightly coupled with the classes like dice, jumper, player, etc. We could have made them loosely coupled through interfaces. But writing interfaces for all the classes here would take a lot of time and we may run out of time for writing this code. What is the expectation in the interview?
@TheTechGranth Ensure that no ladder endpoint coincides with a snake start point, and no snake endpoint coincides with a ladder start point. I think we need to add this conditions.
Sir what if we can make Obstacle as interface and let snakes and ladders implement from it .In future if you want to add any other obstacles we can do that by extending the code i.e open for extension and closed for modification....and one thing sir if we want to invert to rules i.e snakes become ladders and viceversa...i.e when ever you hit the tail of snake you can upwards up to the head...and viceversa for ladders...How do that sir with minimal modifications.? where user select the rules based up on his interest..
1) You can do that as well but then in that case the game will not be snake and ladder :) I felt it will just be a overhead in this scenario, considering you just have to move up or down based on if it is a snake or a ladder but your point will make more sense in case we had some complex algorithm which differentiates snake from a ladder and in future we have possibility to add more obstacle. 2) this is what jumper will do, when you load the board, you have get input for user details and positioning of snake and ladder, just name the jumper as snake and give it the functionality of ladder, no modification required in current code to answer your 2nd point. But then again, even though it is technically feasible, you wont do that in general.
No, low level design can be extended to be a machine coding. In low level design you are supposed to design the api and class and relation between classes using design principles and patterns, futher you can be asked to implement few methods which falls under machine coding
Yes you are right, variable naming is really important while coding in real project and interviews both. Hope it was helpful. Do like and subscribe and share with your friends 🙂
i think there is some mistake in generating diceRoll (((int)( Math.random() * 6*2 - 2)) + 1); if numberOfDice = 2, then 12-2 = 10 + 1 = 11 we never achieve 12. Edit: Instead we can use this (((int)( Math.random() * 6*numberOfDice)) + 1), correct me if i am wrong.
I think you're giving equal probability to all numbers for more than 1 die, but actually, probability of 5 (4+1, 2+3, 3+2, 1+4) is different than say 2 (1+1) Just a thought 😅
Very Good Observation, Infact he should not include number of dice variable in Dice class, because that is not a property of Dice class. It should be a property of Dice Provicer.
i think there is some mistake in generating diceRoll (((int)( Math.random() * 6*2 - 2)) + 1); if numberOfDice = 2, then 12-2 = 10 + 1 = 11 we never achieve 12. Edit: Instead we can use this (((int)( Math.random() * 6*numberOfDice)) + 1), correct me if i am wrong.
Math.random has a syntax of int rand = (int)(Math.random() * range) + min; So i guess it should be (int)(Math.random() * 6*numberOfDice) + 2; Because we can never get 1 Correct me if I am wrong
I am starting with system design, yours is the first video I watched. Awesome tutorial.
There are some modifications:
1. We can use map to store ladder and snakes in the form of map[startPosition]=finalPosition.
2. Instead of creating a queue and popping the front and pushing it to the back. Simply keep a fixed array and keep increment the index in a circular manner i.e index=(index+1)%noOfPlayers thus index will give the current player at any time
true no need to use deque
What happens if some player has won the game... you'll have to skip it's chance as well
what is the benefit of point 2?
Simple approach a good start for beginners to lld(like me) to understand how machine coding would be. Thanks for the video and efforts.
You can use map to store snakes and ladders insted of list. It will save for loop. Also builder pattern will be good to build gameboard.
Builder pattern how??
Could've refactored the if-else cascading at 15:03, the logic looks very reduandant at places. You could've done teh nextposition[0] == nextcell check once after checking for both the ladders and snaked. If (nextposition[0] > nextcell) {// ladder} else {// snake bit}
code may be optimized, instead of creating the list of snake, ladder, and jumper, we can create a map for where the key is start point and value is endpoint, this way we can save time to search if target position is ladder/ snake or not int o(1) time,
after searching on the internet, I think i am in right place for low-level designing
You are right, it is just that the list will not be too big in a game like this, and readability wise it felt better hence my choice of list but time complexity wise you are right.
Hope content are helpful. Do like and subscribe and share with others 🙂
That O(1) optimisation you suggested is very good!
Extremely helpful.
Thanks for the video bro.
Please post more LLD videos
Timestamps
00:04 - Introduction to low-level design of a customizable Snake and Ladder game.
02:19 - Initialize game configuration for Snake and Ladder.
04:32 - Designed a dice class and a generic jumper class for Snake and Ladder game.
06:53 - Implemented game board features for Snake and Ladder.
09:10 - Implemented game mechanics for starting and managing player turns.
11:28 - Game logic manages player positions and winning conditions in Snake and Ladder.
13:41 - Handling player movement for snakes and ladders in the game.
15:58 - Game logic involves players, dice rolls, and handling snakes and ladders.
18:12 - Overview of Snake and Ladder game board design.
Thanks for explaining in such an easy way
Nice and simple explanation. Very helpful
👍💯🙏 Welcome again
Great content @TheTechGranth
Glad it was helpful. Do like and subscribe and share with others 🙂
Great Video. Thanks for that. I have a question.
In your design, the game board is tightly coupled with the classes like dice, jumper, player, etc. We could have made them loosely coupled through interfaces. But writing interfaces for all the classes here would take a lot of time and we may run out of time for writing this code. What is the expectation in the interview?
It would be good if you share the LLD diagram for this.
@TheTechGranth Ensure that no ladder endpoint coincides with a snake start point, and no snake endpoint coincides with a ladder start point. I think we need to add this conditions.
Jump part is little complicated for beginners. Please try making it simpler.
I have one query, when someone ask me to do a low level design, what is the output he is expecting? Code or some diagram
Class diagrams and the code.
Great video brother ❤️❤️
I think the loop in which you are trying to find snake bottom position or ladder jump position could have been done using a hashmap
Yes you are right, thanks for suggestion
ideally getting the current position from the positions map should mapped via the player id instead of the player name
Awesome video!
Respect++
Excellent !
Glad it was helpful. Do like and subscribe and share with others 🙂
Hi, you have any lld of virtual machine creation system design?
Can you elaborate the problem pls
i think you should start from use case diagram, class diagram then come to the actuall code
Sir what if we can make Obstacle as interface and let snakes and ladders implement from it .In future if you want to add any other obstacles we can do that by extending the code i.e open for extension and closed for modification....and one thing sir if we want to invert to rules i.e snakes become ladders and viceversa...i.e when ever you hit the tail of snake you can upwards up to the head...and viceversa for ladders...How do that sir with minimal modifications.? where user select the rules based up on his interest..
1) You can do that as well but then in that case the game will not be snake and ladder :) I felt it will just be a overhead in this scenario, considering you just have to move up or down based on if it is a snake or a ladder but your point will make more sense in case we had some complex algorithm which differentiates snake from a ladder and in future we have possibility to add more obstacle.
2) this is what jumper will do, when you load the board, you have get input for user details and positioning of snake and ladder, just name the jumper as snake and give it the functionality of ladder, no modification required in current code to answer your 2nd point.
But then again, even though it is technically feasible, you wont do that in general.
Thank you sir ..
great one man.
Is low level. Design and machine coding round same?
No, low level design can be extended to be a machine coding. In low level design you are supposed to design the api and class and relation between classes using design principles and patterns, futher you can be asked to implement few methods which falls under machine coding
@@TheTechGranth so low level. Design is subset of machine coding?
@@shubhampokhriyal8491 not exactly, in machine coding, you can be given the class and methods, and you need to code the algorithm
@@TheTechGranth and in low level. Design no. Code?
@@shubhampokhriyal8491 ideally yes but you may be asked to code some methods as well, depends on your years of experience and your interviewer
Bahut badhiya
I think variable b can be renamed as climbed and will make more sense By the ways thanks for explaining
Yes you are right, variable naming is really important while coding in real project and interviews both.
Hope it was helpful. Do like and subscribe and share with your friends 🙂
we can also use else if statements to eliminate the use of variable b I guess
improvement : no of snakes and no of ladders should nt be hard coded . We need to make it randomly for every new game
Good Job Dude.
Thanks. Do check out other videos. Please like and subscribe, and share with your friends :)
I think rollDice will return 2 to 11 if we pass numberOfDice 2?
yes 12 will never achieved with two dice,seems wrong to me
i think there is some mistake in generating diceRoll
(((int)( Math.random() * 6*2 - 2)) + 1);
if numberOfDice = 2, then 12-2 = 10 + 1 = 11 we never achieve 12.
Edit: Instead we can use this (((int)( Math.random() * 6*numberOfDice)) + 1), correct me if i am wrong.
should i really study system design, I am a fresher and getting out of college
I think you're giving equal probability to all numbers for more than 1 die, but actually, probability of 5 (4+1, 2+3, 3+2, 1+4) is different than say 2 (1+1)
Just a thought 😅
Thank you, that is a very valid point and a very good observation.
twitter.com/GranthTech/status/1346469536799682561
@@TheTechGranth Growing together :-)
@@architsri94 Do check out the other videos and provide your valuable feedback. Do not forget to share it with your friends, like and subscribe.
Very Good Observation, Infact he should not include number of dice variable in Dice class, because that is not a property of Dice class. It should be a property of Dice Provicer.
do we have to do this necessarily in java?
i think there is some mistake in generating diceRoll
(((int)( Math.random() * 6*2 - 2)) + 1);
if numberOfDice = 2, then 12-2 = 10 + 1 = 11 we never achieve 12.
Edit: Instead we can use this (((int)( Math.random() * 6*numberOfDice)) + 1), correct me if i am wrong.
but it will reach 13 then?
@@ayushigupta685 i tried but didn't got 13, can you try once.
Math.random has a syntax of
int rand = (int)(Math.random() * range) + min;
So i guess it should be
(int)(Math.random() * 6*numberOfDice) + 2;
Because we can never get 1
Correct me if I am wrong