- Problem Statement
- Noting down the requirements
- Describing the components to be used
- Start by creating the UI
- Describe the config to be passed
- Using config create the state for each box.
- Create a working UI by linking all 3 previous steps.
- Add logic for color change on button click.
- Add logic to store clickOrder in which buttons were clicked.
- Finally add, logic to reset the color of each box to white, once all boxes have been clicked and their color is green.
Create a UI and functionality that looks exactly like shown in this Link.
- The UI should look 'exactly' like shown in the image above with 7 boxes shaped in C shape.
- On click of the box, its color changes to green.
- When all boxes are green, they should again go back to previous color white, one by one, by 1 second delay, in the same order as they were first clicked.
For this I can break the UI into 3 components:
First I'll start by creating the UI. Create different files for all 3 components described above and write CSS for those.
In order to create the UI, I'll pass a config in the following format to the BoxesContainer.
The config is an array of array, with each nested array depicting the row, and each boolean value depicting whether the box is present at that location or not.
Based on this config, I'll create a global state, which contains state for each box, which has an
isClicked attribute. The state will look like this.
At this stage our code looks like as shown in the document below:
We've created 3 components BoxesContainer, BoxesRow, Box, and also written the CSS for that. And with the config we got as the input, we created the boxesState.
CodeSandbox Link 1
Now since our UI is ready, let's add the logic, of color change on box click.
CodeSandbox Link 2
We also need to maintain the clickOrder of different boxes. This will be used when we need to reset the box color one by one, when when all boxes color has been changed to green.
CodeSandbox Link 3
Now finally, we need to implement the logic, where once all the boxes are green, it should reset all boxes to white, in the same order in which they were clicked, one by one, by 1 second delay.
CodeSandbox Link 4
- Although I was able to give a working solution to this problem within around 45-50 mins, my code lacked scalability and reusability.
- So initially, when I heard from the interviewer, that I need to create the exact same UI, I simply created divs for every box. I didn't think about the config. So even if the requirement is small, we should plan to write our code in such a way that later, it should be reusable and as configurable as possible.
- During the interview, I had to change my state logic several times. Initially I was unable to come up with what the final state of the app will look like, and which state will be maintained at the child level and which will be maintained at parent level. So, this wasted some precious minutes in the interview.
- I was finally able to come up with this code solution after 3 attempts. In this code too, there are some places where DRY principles can be used to improve the code quality.