Coding workouts
Improving team hard skills
To reach mastery, practice is the best. Coding is not different, so every developer should do some exercises periodically.
A better way to practice and adopt the mental ability to solve problems is by solving problems. But what kind of problems?
Coders can practice and improve their coding skills with katas. Coding Katas was coined by Dave Thomas, co-author of “The Pragmatic Programmer,” from the Japanese martial arts where warriors practice their movements over and over until they domain them.
A coding kata solves a trivial problem with low essential complexity but lets developers share different versions of the solution and learn new ways to do it in terms of efficiency and performance.
We developers love variety and learning new things constantly. Friday is an excellent moment to perform coding katas with the team; near of weekend could be a little more relaxed and good to enjoy learning and encourage them to practice the new knowledge the following week.
Following, I present some kata exercises to practice coding skills with Javascript. The goal is to get a better version of the problem solution.
Some recommendations:
- Understand the exercise.
- Check the test criteria. What is the input? What is the output?
- Make a first version that meets the test criteria.
- Improve the version to learn new concepts.
Creating a fake dataset
A program that given a number N input, returns a JSON array size N or random objects with properties like name and phone numbers with consistent values. No libraries or npm packages can be used.
Input: Number of records to generate (N)
Output: JSON array with N different records
Goal: use of arrays, single responsibility principle, and make data from a few seeds.
One version of the solution:
Searching array elements into another array
A program that returns an array with an id and name from another big array (1 million elements) dataset when ids in the input array (1,000 elements) are a match.
Input:
- 1,000,000 elements array with {name, phone, id}
- 1,000 elements array with idsOutput: JSON array with matched elements
Goal: order of magnitude algorithms, time complexity concept, improving performance with hash tables, and Map object use.
Solution 1: O(MN) search using arrays methods, if M is the large array size and N is the small array size.
Results:
Search array elements in a big array
Size of data: 1,000,000
Size of elements to search: 1,000
[...]
Memory usage: 183 MB
Time of process: 1.308s
Solution 2: O(M)+O(N) search using Map object; O(M) is the map creation, and O(N) is the searching.
Results:
Search array elements in a a big data Map
Size of data: 1,000,000
Size of elements to search: 1,000
[...]
Memory usage: 310 MB
Time of process: 345.812ms
So now, if we run these couple of programs for searching 10,000 elements in a 1 million elements array. The results will be even more meaningful.
Solution 1 results: O(MN) search
Search array elements in a a big data Map
Size of data: 1,000,000
Size of elements to search: 10,000
[...]
Memory usage: 186 MB
Time of process: 12.710s
Solution 2: O(M)+O(N) search
Search array elements in a a big data Map
Size of data: 1,000,000
Size of elements to search: 10,000
[...]
Memory usage: 310 MB
Time of process: 346.414ms
Conclusion
Coding katas are a pretty good instrument to promote knowledge transfer and excellence at work in software engineering teams. Tech leads and engineering managers can lead this kind of exercise as part of the maturity process of the hard skills of the team.
Prepare your katas with a specific goal according to your team's needs. Some useful links with problems proposals are: