<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[G33k Chronicles]]></title><description><![CDATA[Software Engineer | CMU Alum | I write about web and mobile technologies as well as professional and personal well-being]]></description><link>https://meekg33k.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 23:12:47 GMT</lastBuildDate><atom:link href="https://meekg33k.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Getting Started In Tech. What You Need to Know]]></title><description><![CDATA[So tell me, did a friend or someone you know talk about tech or did someone on Twitter recently announce a mouth-watering job offer from a tech company? Or you are jaded with your current job and are looking to make the transition to tech? 
And you’r...]]></description><link>https://meekg33k.dev/getting-started-in-tech-what-you-need-to-know</link><guid isPermaLink="true">https://meekg33k.dev/getting-started-in-tech-what-you-need-to-know</guid><category><![CDATA[tech ]]></category><category><![CDATA[General Advice]]></category><category><![CDATA[Career]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Fri, 11 Feb 2022 19:53:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644558812662/ODQDaR5kq.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So tell me, did a friend or someone you know talk about tech or did someone on Twitter recently announce a mouth-watering job offer from a tech company? Or you are jaded with your current job and are looking to make the transition to tech? </p>
<p>And you’re wondering (rightly so), how to get in on the act? You're asking yourself, <em>"how can I position myself to take advantage of this ‘tech’ thing? How can I get my own share of the insane dollars flying around in tech? How do I start my tech career?"</em></p>
<p>If these are some of the questions you have been asking, then no worries my friend, because I have some answers for you. The one thing I ask of you is that you stay open-minded and inquisitive while reading this article. </p>
<p>Ready to dive in?</p>
<p>Let’s start with what tech is. Really, what is tech?</p>
<h2 id="heading-what-tech-is">What tech is</h2>
<p>First off, the word <em>tech</em> is a less mouthful, mono-syllabic abbreviation for the word <em>technology</em>. </p>
<p>See? Easy peasy, can we all go home now? Well, not yet, because we haven’t talked about what technology is. </p>
<p>A quick Google search will show you tons of definitions of what technology is and here is one:</p>
<p><em>Technology is simply science or knowledge put into practical use to solve problems or invent useful tools</em>.</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Technology">Wikipedia</a> has a more interesting definition:</p>
<p><em>Technology is the sum of any techniques, skills, methods, and processes used in the production of goods or services or in the accomplishment of objectives.</em></p>
<p>Keep these definitions in mind because I will make a reference to them later on in this article.</p>
<h2 id="heading-what-tech-isnt">What tech isn’t</h2>
<p>I just shared a simple but effective explanation of what technology is and you might be wondering, “<em>why do I need to know what technology is not since I already know what it is</em>?”. </p>
<p>I like that you are asking that - we promised to stay inquisitive and you are coming through! So, I’ll tell you why with a short story.</p>
<p>Several blind men encountered an elephant. The blind man who touched the trunk, said it felt like a snake. The blind man who touched one of the legs of the elephant said it was a tree. Are you already getting the picture?</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644563934240/0oziOupGy.png" alt="elephant.png" /></p>
<blockquote>
<p>Source: https://www.mdhadallas.org/blog/the-blind-men-and-the-elephant-how-an-ancient-parable-explains-the-need-for-shared-data/</p>
</blockquote>
<p>So if you happened to meet the blind man who touched the legs of the elephant and asked him what the elephant was, he would describe it as a tree. Similarly the one who touched the trunk, would describe the elephant as a snake, a big snake.</p>
<h2 id="heading-why-am-i-saying-all-this">Why am I saying all this?</h2>
<p>It is in our human nature to overemphasize certain parts of a thing that we have experienced oftentimes at the expense of the other parts, that we haven’t experienced or do not know about.</p>
<p>A popular misconception flying around these days is that tech is coding. Did I hear you ask, <em>what is coding</em>? Thanks again for staying inquisitive.</p>
<p>Coding is essentially a conversation between a programmer (often a human being) and a computer asking the computer to carry out some certain tasks.</p>
<p>If you need someone to help you with a task, you would tell the person the details of the task and ask the person in a language the person understands. Coding is really no different from that. </p>
<p>Now we know what coding is about, here is what I want to say: </p>
<blockquote>
<p>Coding is Tech but Tech Isn’t Only Coding</p>
</blockquote>
<p>Read that again and understand it very well.</p>
<p>While coding is essentially the part where the computer is instructed to do certain things, tech is so much more than just coding. It is the <strong><em>sum of any techniques, skills, methods, and processes</em></strong> (remember the definition from Wikipedia) to produce a good or a service.</p>
<p>Essentially, tech is:</p>
<ul>
<li>Techniques</li>
<li>Skills (coding being just one of them, but there are others)</li>
<li>Methods</li>
<li>Processes</li>
</ul>
<p>So when you say you want to get started in tech, it doesn’t <strong>always</strong> mean that you have to learn coding (I’ll be happy if you decide to because I’m a coder myself). </p>
<p>You can find your path by gaining mastery and knowledge of any of the other techniques, skills, methods and processes required in tech.</p>
<h2 id="heading-so-how-do-i-find-my-path">So how do I find my path?</h2>
<p>I know you have questions but first, let’s take a moment to acknowledge that you have already made some progress just by reading this article and your journey into tech has started.</p>
<p>Now you know what tech is and what it isn’t, a recommended next step would be knowing what paths to follow in tech because like I mentioned earlier, tech is vast and I believe everyone who’s genuinely interested can find their place in it. </p>
<p>It’s okay to roll your eyes at me because I’m about to go all motivational on you. 😄</p>
<p>I recently co-hosted a Twitter Space (remove link because no recording) where I talked about the different non-coding career paths for people looking to get started in tech. In that talk, I shared some tips, which I’ll share now, that can help you determine what career path you should follow.</p>
<p>One thing to note here is that these are not absolutes, they are guides. So here are the tips:</p>
<ul>
<li><p><strong>Choose a path that aligns with your core strengths</strong>: If you find yourself drawn to abstract concepts, there's a chance you might be excel better in coding and other related career paths. If you have a thing for art and design, maybe being a User Interface/User Experience designer might be a good fit.</p>
</li>
<li><p><strong>Choose a path that you are passionate about and very interested in</strong>: If you are looking to build a career that will last, it is germane you choose a path that you are interested in, because that will help keep you going when the hurdles come because they will. And I just want to add that if all you’re interested in is the money, that’s okay too so long as you find fulfillment in that.</p>
</li>
<li><p><strong>Choose a path that leverages your previous experience</strong>: This might be more helpful for people looking to make a transition from a non-tech career into tech. Again these aren’t absolutes, but I’ve seen them work for other people and it just makes the transition easier.</p>
</li>
</ul>
<p>With that said, let me now share some different career paths in tech.</p>
<h2 id="heading-career-paths-in-tech">Career Paths in Tech</h2>
<p>This is by no means an exhaustive list of career paths in tech but I believe it is decent enough to serve as a guide to helping you make a decision.</p>
<p>I would have loved to talk a bit more about each of these paths but that would make this article too long; so instead I added links to each of the paths listed here so you can read more about them if you are interested - remember we said interest is key to helping you find your path.</p>
<h3 id="heading-coding-related-paths">Coding-Related Paths</h3>
<p>Here are paths in tech that will have you holding conversations with a computer:</p>
<ul>
<li><p><a target="_blank" href="https://www.interviewbit.com/blog/backend-developer-skills/">Back-end Development</a>: It involves building the behind-the-scenes services that power web/mobile applications.</p>
</li>
<li><p><a target="_blank" href="https://www.ibm.com/topics/what-is-blockchain">Blockchain</a>: is a shared, immutable ledger that facilitates the process of recording transactions and tracking assets in a business network.</p>
</li>
<li><p><a target="_blank" href="https://www.ibm.com/topics/cybersecurity">Cybersecurity</a>: It describes the practice of protecting computers and critical systems from malicious attacks.</p>
</li>
</ul>
<ul>
<li><p><a target="_blank" href="https://www.coursera.org/articles/what-does-a-data-engineer-do-and-how-do-i-become-one">Data Engineering</a>: This focuses on using scientific methods and systems to extract insights from data.</p>
</li>
<li><p><a target="_blank" href="https://ischoolonline.berkeley.edu/data-science/what-is-data-science/">Data Science</a>: This entails building the pipelines and systems for the storage and passage of data at scale.</p>
</li>
<li><p><a target="_blank" href="https://www.redhat.com/en/topics/devops/devops-engineer">DevOps Engineering</a> / <a target="_blank" href="https://www.coursera.org/articles/what-is-a-cloud-engineer">Cloud Engineering</a> / <a target="_blank" href="https://www.jobhero.com/career-guides/interviews/prep/what-is-an-infrastructure-engineer">Infrastructure Engineering</a>: Sometimes used interchangeably, this career path focuses on building and maintaining the pipelines and infrastructure used by applications.</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-game-development/">Game Development</a>: This focuses on development of design and development of games across different platforms.</p>
</li>
<li><p><a target="_blank" href="https://www.ibm.com/cloud/learn/machine-learning">Machine Learning</a> / <a target="_blank" href="https://www.ibm.com/cloud/learn/what-is-artificial-intelligence">Artificial Intelligence</a>: Machine learning (often viewed as a subset of artificial intelligence), but they both focus on creating intelligent systems that keep learning.</p>
</li>
<li><p><a target="_blank" href="https://dzone.com/articles/what-is-mobile-development">Mobile Development</a>: As a mobile developer, you will be tasked with development of applications for mobile devices</p>
</li>
<li><p><a target="_blank" href="https://www.newhorizons.com/resources/d/blog/how-to-become-a-network-engineer">Network Engineering</a>: Network engineers design, build and maintain computer networks for organizations.</p>
</li>
<li><p><a target="_blank" href="https://www.atlassian.com/continuous-delivery/software-testing/automated-testing">Software Testing (Automated)</a>: It involves using software tools to automate a manual process of software testing.</p>
</li>
<li><p><a target="_blank" href="https://www.moveoapps.com/blog/web-application-development-guide/">Web Development</a> (<a target="_blank" href="https://frontendmasters.com/guides/front-end-handbook/2018/what-is-a-FD.html?">Front-end</a>): This involves developing applications to be viewed via a <a target="_blank" href="https://en.wikipedia.org/wiki/Web_browser">web browser</a> with a focus on building the user interface of the web application.</p>
</li>
<li><p><a target="_blank" href="https://www.lewagon.com/tech-jobs/web-development/full-stack-developer">Web Development (Full-stack)</a>: This combines both front-end and back-end development defined above.</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-web3/">Web3 Development</a>: The hottest kid on the block, this focuses on building <a target="_blank" href="https://www.oreilly.com/library/view/decentralized-applications/9781491924532/ch01.html">decentralized applications</a>  that run on either blockchain or other decentralized networks.</p>
</li>
</ul>
<h3 id="heading-non-coding-paths">Non-Coding Paths</h3>
<p>Also, here are paths in tech that won't require you having conversations directly with a computer:</p>
<ul>
<li><p><a target="_blank" href="https://www.simplilearn.com/data-analysis-methods-process-types-article">Data Analysis</a> (some cases): This involves collecting, cleaning and interpretation of data to draw insights. This is often the entry path for people looking to go into data science.</p>
</li>
<li><p><a target="_blank" href="https://www.ziprecruiter.com/Career/Privacy-Manager/What-Is-How-to-Become">Data Privacy Management</a>: This focuses on helping organizations maintain compliance with data privacy laws and regulations.</p>
</li>
<li><p><a target="_blank" href="https://thenewstack.io/devrel-for-beginners-how-to-get-started/">Developer Advocacy/Developer Relations</a> (some cases): This role focuses on building excellent relationships with developers, who mostly use a company’s products or services.</p>
</li>
<li><p><a target="_blank" href="https://blog.hubspot.com/marketing/what-is-digital-marketing">Digital Marketing</a>: Sometimes referred to as <em>online marketing</em>, it entails the promotion of goods and services using the internet and other forms of digital channels. Think of it as marketing powered by tech.</p>
</li>
<li><p><a target="_blank" href="https://theproductmanager.com/topics/what-is-product-management/">Product Management</a>: This career path focuses on planning, creating, managing and launching products and services within an organization.</p>
</li>
<li><p><a target="_blank" href="https://monday.com/blog/project-management/guide-to-project-management/">Project Management</a>: It describes the use of specific knowledge, skills, tools and techniques to ensure delivery of something of value to people.</p>
</li>
<li><p><a target="_blank" href="https://www.searchenginejournal.com/seo-guide/">Search Engine Optimization</a> / <a target="_blank" href="https://www.semrush.com/blog/search-engine-marketing/">Search Engine Marketing</a>: Often used together, they both describe a form of internet marketing that involves the promotion of websites by increasing their visibility in search engines.</p>
</li>
<li><p><a target="_blank" href="https://www.guru99.com/manual-testing.html">Software Testing (Manual)</a>: In contrast with automated testing, this describes a manual process of testing software without the use of automated tools.</p>
</li>
<li><p><a target="_blank" href="https://codingcareer.circle.so/c/dx-blog/technical-community-builder-is-the-hottest-new-job-in-tech">Tech Community Management</a>: This career path focuses on building, managing and growing tech communities.</p>
</li>
<li><p><a target="_blank" href="https://www.lawstudies.com/article/what-is-technology-law-and-why-should-you-study-it/">Tech Law / Intellectual Property Law</a>: The body of law that governs the use of technology, with IP law being one of the hottest paths. So you can think of this as law but with focus on tech.</p>
</li>
<li><p><a target="_blank" href="https://www.hackerearth.com/blog/talent-assessment/technology-recruiting/">Tech Recruiting</a>: This comprises sourcing, screening, and assessing candidates who fit specific technical roles</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/how-to-become-a-technical-writer/">Technical Writing</a>: Technical writers are professional communicators who seek to transfer technical knowledge to their audience through writing.</p>
</li>
<li><p><a target="_blank" href="https://www.interaction-design.org/literature/topics/ux-design">User Experience Design</a>: This focuses on creating products that provide meaningful and relevant experiences to users.</p>
</li>
<li><p><a target="_blank" href="https://www.interaction-design.org/literature/topics/ux-research">User Experience Research</a>: A UX researcher studies would-be users of a product, to understand their requirements and add these insights into the design of the product. </p>
</li>
<li><p><a target="_blank" href="https://www.interaction-design.org/literature/topics/ui-design">User Interface Design</a>: While a UX designer focuses on experience, a UI designer designs interfaces for applications with focus on look and style.</p>
</li>
</ul>
<p>I tried to not overload you with a lot of information, believe me I did. One thing you should see however, is that there are a good number of career paths that don't require coding, if you believe coding is not your thing (I'll be curious to know why though).</p>
<h2 id="heading-next-steps">Next Steps</h2>
<p>By reading to this point, I assume that you have found a path you want to follow. Now it's time to take concrete steps to follow that path.</p>
<p>Here are some recommended next steps:</p>
<h3 id="heading-research-more-on-your-chosen-path">Research more on your chosen path</h3>
<p>I highly recommend that you do some good research about the path you have chosen.
Researching more about your chosen path will give you a better understanding of what the path entails but more importantly, it will paint to you a clear picture of the gulf between where you are and where you want to be - that should be the goal.</p>
<p>Like I mentioned earlier, I have added links to the different career paths. These links are a good first place to begin your research, but you should go beyond just reading the articles I've shared.
I highly recommend speaking to someone who's an expert in your chosen field. If this is something you are interested in, you can complete this <a target="_blank" href="https://bit.ly/sidebar-with-a-techie">form</a> where we will try to match you with someone who is an expert in your field of interest.</p>
<h3 id="heading-develop-a-learning-plan">Develop a learning plan</h3>
<p>So you have researched your chosen tech career path, you are very excited and you can't wait to get started. I'm not here to dampen that excitement because I'm also excited for you, but I want to highlight the importance of developing a learning plan.</p>
<p>A learning plan essentially is your system for getting you from where you are to where you want to be (which ties into the output of the research step).</p>
<p>Another thing a learning plan will help you do is to keep you going on those days when the excitement seems to have faded because there's a high chance that those days will come.</p>
<p>For easy reference, here's a sample of what my learning plan looked like at the time I wanted to learn about a certain technology - Docker:</p>
<ul>
<li>Read a new article on Docker every week (LEARN)</li>
<li>Spend 1 hour every day writing Docker commands (DO)</li>
<li>Talk to someone who is a Cloud/Infrastructure Engineer (CONNECT)</li>
</ul>
<p>The last thing I want to mention here is that your learning plan is a living document, so you should keep updating it as you get more information.</p>
<h3 id="heading-just-start">Just start</h3>
<p>As Nike says, "<em>just do it</em>".</p>
<p>Once you've developed a learning plan, just start. Starting might mean enrolling in a course or joining a community of other learners but hey, just start.</p>
<p>And don't worry if you feel dumb at the onset, all masters were once dumb when they started and here's a quote I find very interesting:</p>
<blockquote>
<p>"<em>Embarrassment is the cost of entry. If you aren't willing to look like a foolish beginner, you'll never become a graceful master</em>".</p>
</blockquote>
<p>Need I say more?</p>
<h3 id="heading-set-measurable-goals-to-track-your-progress">Set measurable goals to track your progress</h3>
<p>Yes, you're going to learn but you also need to set goals (think of them as milestones) to track your progress.</p>
<p>I recommend breaking down your goals into items so that you can act on them every day, rather than waiting till the end of the month or year to achieve your goal.</p>
<p>Here are some examples of measurable goals:</p>
<ul>
<li>Complete a course module every day.</li>
<li>Publish an article about what you have learned every week.</li>
<li>Contribute to an open-source project every month.</li>
</ul>
<h3 id="heading-get-an-accountability-partner">Get an accountability partner</h3>
<p>Life is better done with another person so on this journey, I'll recommend finding someone to keep you accountable. This can be a friend, a mentor - preferably a buddy, or even a community.</p>
<p>Whatever you choose should be based on your preferences and what works best for you. Don't be caught doing something because everyone else is doing it, it should work for you.</p>
<p>If after reading through this article, you are still unsure of what path you want to follow - that's absolutely fine. I'm happy to schedule a one-on-one chat with you, you can use this <a target="_blank" href="https://calendly.com/meekg33k/tech-chat-with-uduak">link</a> or send me a <a target="_blank" href="https://www.twitter.com/meekg33k">DM on Twitter</a>.</p>
<h2 id="heading-some-final-words">Some final words</h2>
<p>As we conclude, let me share a personal story.</p>
<p>I went <strong>scuba-diving</strong> for the first time last year and one of the things I was told in preparation for that adventure was that scuba-diving was a lot easier if one knew how to swim. But more importantly, to enjoy scuba-diving, I had to wear the right kit to enable me to breathe more easily so I could navigate the beautiful ocean depths.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644572284530/YniRM1gF9.jpeg" alt="scuba-diving.jpg" /></p>
<p>With the loads of information out there about tech, I hope with this article, I have given you all the courage, motivation and most importantly, the right knowledge (kit) you need before you go <strong>scuba-diving</strong> into the vastness that tech is.</p>
<p>I'm super excited that you have considered getting into tech because there are a ton of problems that we can solve in our world today. I could go on and on writing this article but I would rather pause here and have you channel your energy into taking the next steps as soon as possible.</p>
<p>So here's me, your number one fan rooting for you. I wish you all the best in your career.</p>
<p>You've got this!</p>
<p><em>In my next article, I'll be recommending some free resources - courses, articles and more, to help you get started in selected tech career paths. Please let me know which ones you are interested in the comment section so I can include them in the article.</em></p>
]]></content:encoded></item><item><title><![CDATA[4 Questions Good Software Engineers Ask During a Technical Interview]]></title><description><![CDATA[Imagine you find yourself in a technical interview where you are given a problem; be it an algorithm design problem, a system design problem, or enhancing an existing feature. What will you do first? 
Will you immediately dive into solving the proble...]]></description><link>https://meekg33k.dev/4-questions-good-software-engineers-ask-during-a-technical-interview</link><guid isPermaLink="true">https://meekg33k.dev/4-questions-good-software-engineers-ask-during-a-technical-interview</guid><category><![CDATA[Technical interview]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[data structures]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Fri, 07 May 2021 05:57:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1620364010620/pCDcofP98.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Imagine you find yourself in a technical interview where you are given a problem; be it an algorithm design problem, a system design problem, or enhancing an existing feature. What will you do first? </p>
<p>Will you immediately dive into solving the problem or will you zone out as you try to think through the problem? </p>
<p>More often that not, a lot of people immediately go straight into trying to solve the problem. Some others will zone out in a bid to think through the problem while the good software engineers start by asking questions in a bid to understand the problem better because they know and understand the importance of asking the right questions and the role it plays in helping to develop the most optimal solution to a given problem.</p>
<p>You are a good software engineer, and that's why you are reading this article. </p>
<p>And so in this article, I will highlight 4 important questions that good software engineers ask during technical interviews. Using this knowledge, we will then see how this technique was/was not applied by the 30+ folks who took on the algorithm challenge for <a target="_blank" href="https://twitter.com/meekg33k/status/1388179391390359563">Week 4 of Algorithm Fridays</a>.</p>
<p>It is important to note that these questions are not just suited for technical interview scenarios. Since technical interviews are mostly around solving problems, the lessons learned here can be applied to any problem-solving situation, which happens even while on the job.</p>
<p>So let's get started.</p>
<h3 id="conversation-over-correctness">Conversation over Correctness</h3>
<p>If you were asked what the main value in a technical interview is, would you say the value is in being able to solve the problem you are given or would you say the value is in the conversation that happens between you and your interviewer.</p>
<p>Which would you pick?</p>
<p>One of the things I learned from doing <a target="_blank" href="https://meekg33k.dev/what-i-learned-from-doing-60-technical-interviews-in-30-days">60+ technical interviews in 30 days</a> is that the real value in technical interviews is not the correctness of your solution or your ability to come up with a solution, it's the conversation that happens during those interviews.</p>
<p>Did I hear you say "wait, what?"</p>
<p>I know what you just read may seem counter-intuitive at a first glance but think about it.</p>
<p>I should however mention that by technical interviews, I am not referring to the 45-minute coding challenges, from Hackerrank or the likes. I'm referring to interviews where you engage with another person while solving a technical problem.</p>
<p>For those kind of interviews, yes the conversation that happens between you and the interviewer is more valuable than just solving the problem.</p>
<p>One reason for this is because more often than not, when a technical problem is presented to you, there might be some assumptions that you may have made about the problem. So, it is important to ask questions to validate or invalidate your assumptions before jumping right in to solve the problems. </p>
<p>Not doing so could prove costly in terms of time and effort as was the case during one of my interviews where I had assumed the input type, and had developed a solution only to find out later that it was a different type. Needless to say that my assumption cost me the interview because the amount of time I had spent in trying to develop the initial solution was gone.</p>
<p>Another thing to note is that, when you ask the right questions, you are indicating to your interviewer that not only do you recognize the need to clarify the problem statement, you also are a team-player. </p>
<p>Here's an excerpt from the article I wrote on the <a target="_blank" href="https://meekg33k.dev/what-i-learned-from-doing-60-technical-interviews-in-30-days">lessons I learned from doing 60+ technical interviews in 30 days</a>:</p>
<blockquote>
<p>Yes, your interviewer wants to see that you can come up with a solution, but one thing you must not forget is that they also want to see that you can collaborate with other team-mates to come up with a solution. While companies want rock-stars, they also want team-players.</p>
</blockquote>
<p>I hope that by now you are already beginning to see the value in asking questions. I also imagine that you are already asking, what are some of the questions you could ask. That's exactly what I'll be sharing in the next section.</p>
<p>Walk with me. 🚶🏽‍♂️</p>
<h3 id="questions-to-ask-your-interviewer">Questions to Ask Your Interviewer</h3>
<p>Good software engineers understand that part of developing a solution to a problem is testing every assumption you have made about the problem. </p>
<p>So here are examples of questions that good software engineers ask:</p>
<h4 id="1-questions-about-the-input-data">1. Questions about the input data</h4>
<p>Good software engineers ask clarifying questions about the data to be processed. For example, if you are asked to write a function that transforms some input, you should ask the following questions:</p>
<ul>
<li><p>What is the data type?</p>
<ul>
<li>Is it a string, a number, an object?</li>
<li>If it is an array for example, can the array be empty? Can the array contain different types of elements? For example in JavaScript, can you have a string and a number in the array?</li>
</ul>
</li>
<li><p>What is the data size?</p>
<ul>
<li>How big is the input data that we are processing? </li>
<li>For example, if it is a number, how big can the number be? This is very important to know because some operations can't be carried due to overflow errors if the number is very big.</li>
</ul>
</li>
</ul>
<h4 id="2-questions-about-robustness">2. Questions about robustness</h4>
<p>As a good software engineer, your aim should be to develop a system that is robust, can handle unexpected/invalid input without failing. </p>
<p>Some questions to ask include:</p>
<ul>
<li>Can the input data we are processing have some unexpected values like <code>null</code> or could they be <code>undefined</code>?</li>
<li>How should we handle runtime errors? It is important to always check in with the interviewer to note how he/she wants you to handle the errors so that you don't spend a lot of time on it. For most interviews I've had, it was mostly enough to just talk about how you will handle the errors; the interviewer rarely asked me to code it up. However in some other ones, the interviewer wanted to see how I would make my solution more robust.</li>
</ul>
<h4 id="3-questions-about-implementation-and-trade-offs">3. Questions about implementation &amp; trade-offs</h4>
<p>Good software engineers know that every solution comes with a cost and there are always trade-offs. If you design an algorithm using arrays instead of hash maps, there is a cost to it. It is important that you understand these and its trade-offs so you can ask questions around this.</p>
<p>This category of questions is mostly important for system design interview questions.</p>
<p>Here are some sample questions to ask:</p>
<ul>
<li>Is there a reason why one implementation method is preferred over another? For example, recursion over iteration? This may give you deeper insight about the problem.</li>
<li>Is there a data structure you would prefer? One reason to ask this question could be to demonstrate your knowledge of different data structures that could be used for solving a problem.</li>
<li>Is there an acceptable performance threshold? This is a really important question because while we can hope for the most-optimal and perfect systems, it is rarely possible in practice. So it is important to clarify what is tolerable for the solution you are designing. For example, if it is okay to develop a <code>O(N log N)</code> solution, or can we build a system with a 5% latency? This would help you decide on what implementation methods to use in developing the solution.</li>
</ul>
<h4 id="4-questions-about-scale">4. Questions about scale</h4>
<p>Not all interviewers may need you to implement scalable solutions but most certainly, all interviewers will be impressed when you ask questions about scale.</p>
<p>These set of questions basically focus on clarifying how your system should behave when the amount of data to be processed as input increases significantly, or if your system </p>
<p>Examples of some questions to ask include:</p>
<ul>
<li>Do we expect that the input can increase significantly? For example if you had a in-memory solution that worked for processing 100 entries, you may want to rethink that if you are processing a billion entries.</li>
</ul>
<p>Having gone through some of these questions, let's look at how we can apply this to an algorithm problem that was posed to participants in Week 4 of Algorithm Fridays.</p>
<h3 id="sample-application">Sample Application</h3>
<p>The image below shows the algorithm problem for Week 4 of Algorithm Fridays. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1620106196719/Z8e31poVy.png" alt="Algorithm Fridays Week 4.png" /></p>
<p>Applying what we have learned, what are some of the questions we could ask about this problem? Anybody?</p>
<ol>
<li>What is the expected input type? 
That's answered already in the question - an array.</li>
<li>Can the array contain different types? 
Again, that's answered already in the question - an integer array, so we know the array is made of numbers.</li>
<li>Can the input be <code>null</code> or <code>undefined</code>? The answer to this question is yes, because we have no control over what values are passed into the function. This tells you that your solution should be robust enough to handle unexpected/invalid input cases.</li>
<li>What kind of numbers does the array contain?</li>
</ol>
<p>This last question is really important because if you assumed that the input array was made of positive numbers only, you would come up with a solution like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> productExceptSelf = <span class="hljs-function">(<span class="hljs-params">nums = []</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (!nums || nums.length == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> []

  <span class="hljs-comment">// Step 1: Find the product of all the numbers first</span>
 <span class="hljs-keyword">const</span> product = nums.reduce(<span class="hljs-function">(<span class="hljs-params">acc, num</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> acc * num
  }, <span class="hljs-number">1</span>);

  <span class="hljs-comment">// Step 2: Loop through each entry in the array and divide the product by the entry</span>
  nums.forEach(<span class="hljs-function">(<span class="hljs-params">num, index</span>) =&gt;</span> {
     nums[index] = product / num;
  });

  <span class="hljs-keyword">return</span> nums;
}
</code></pre>
<p>The problem with this solution is that when you have an input array where one of the elements has a value of <code>0</code>, for example <code>nums = [4, 3, 0]</code>, this solution fails.</p>
<p>Can you see the reason why?</p>
<p>If one of the entries of the array is <code>0</code>, the <code>product</code> of all the of all the numbers of in the array will be <code>0</code>, leading to the result (in JavaScript) shown below: </p>
<pre><code class="lang-js">productExceptSelf ([<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>]); ❌ <span class="hljs-comment">// should return [0, 0, 12] but returns [0, 0, NaN]</span>
</code></pre>
<p>Now we know we should handle the edge case for when one of the elements is <code>0</code>, but before we rewrite our solution to handle the zero check, let's think about the problem a bit more deeply.</p>
<p>What if our array has more than one element whose value is <code>0</code>, will that affect the final solution we come up with?</p>
<p>Yes, because if we have an array with at least two elements with a <code>0</code> value, the final result should be an array whose elements are all <code>0</code>. How? Let's look at the image below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1620365548430/rzrGB5bv8.png" alt="Two+ 0-element array.png" /></p>
<p>The image shows us for each position <code>i</code> in the result array, the product of all other numbers except <code>nums[i]</code> will always be <code>0</code>. So we can deduce that when there are at least two elements in <code>nums</code> with a <code>0</code> value, every element in the final results array will be <code>0</code>, thus eliminating unnecessary work.</p>
<p>Taking into account all of these, we can come up with the final solution shown below:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> productExceptSelf = <span class="hljs-function">(<span class="hljs-params">nums</span>) =&gt;</span> {

   <span class="hljs-keyword">if</span> (!nums || nums.length == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> [];

   <span class="hljs-keyword">let</span> countOfZero = <span class="hljs-number">0</span>; <span class="hljs-comment">// track the number of zero elements</span>

  <span class="hljs-comment">// Step 1: Find the product of all the numbers except zero</span>
   <span class="hljs-keyword">const</span> product = nums.reduce(<span class="hljs-function">(<span class="hljs-params">acc, num</span>) =&gt;</span> {
      <span class="hljs-keyword">if</span> (num === <span class="hljs-number">0</span>) {
          countOfZero++;
          <span class="hljs-keyword">return</span> acc; <span class="hljs-comment">// Skip multiplying if the element is 0</span>
      }
      <span class="hljs-keyword">return</span> acc * num
    }, <span class="hljs-number">1</span>);

    <span class="hljs-keyword">if</span> (countOfZero &gt; <span class="hljs-number">1</span>) {
      <span class="hljs-comment">// Every element in the result array will be 0</span>
      nums.forEach(<span class="hljs-function">(<span class="hljs-params">num, index</span>) =&gt;</span> {
           nums[index] = <span class="hljs-number">0</span>;
      });
      <span class="hljs-keyword">return</span> nums;
    }

    <span class="hljs-comment">// Step 2: Loop through each element in the array and</span>
   <span class="hljs-comment">// populate the results array based on the value of the element</span>
    nums.forEach(<span class="hljs-function">(<span class="hljs-params">num, index</span>) =&gt;</span> {
      <span class="hljs-keyword">if</span> (countOfZero === <span class="hljs-number">1</span>) {
          <span class="hljs-keyword">if</span> (num === <span class="hljs-number">0</span>) {
            nums[index] = product;
        }
        <span class="hljs-keyword">else</span> {
            nums[index] = <span class="hljs-number">0</span>;
        }
      }
      <span class="hljs-keyword">else</span> {
          <span class="hljs-comment">// There are no elements with a 0 value in the array</span>
        nums[index] = product / num;
      }
    });

    <span class="hljs-keyword">return</span> nums;
}

<span class="hljs-comment">// Test case 1: When the array has no elements with a 0 value</span>
<span class="hljs-built_in">console</span>.log(productExceptSelf([<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>])); <span class="hljs-comment">// ✔️ returns [6, 8, 12]</span>

<span class="hljs-comment">// Test case 2: When the array has only one element with a 0 value</span>
<span class="hljs-built_in">console</span>.log(productExceptSelf([<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>])); <span class="hljs-comment">// ✔️ returns [0, 0, 12]</span>

<span class="hljs-comment">// Test case 3: When the array has only one element with a 0 value</span>
<span class="hljs-built_in">console</span>.log(productExceptSelf([<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>])); <span class="hljs-comment">// ✔️ returns [0, 0, 0, 0]</span>
}
</code></pre>
<h3 id="winners-of-the-dollar20-award">Winners of the $20 award</h3>
<p>After reviewing almost solutions in 5 different programming languages, the $20 award for Week 4 of Algorithm Fridays goes to <a target="_blank" href="https://www.twitter.com/Flappizy">@Flappizy</a> and <a target="_blank" href="https://www.twitter.com/thisaliaba">@thisaliaba</a>, and you can find their solutions <a target="_blank" href="https://gist.github.com/Flappizy/8412e39b804e38951973465cbadd5e41">here</a> and <a target="_blank" href="https://gist.github.com/alialaba/3da8855eb92e47c6b36e23baea7645c0">here</a> respectively.</p>
<p>We picked these solutions because apart from being correct and robust, they used a very intelligent approach in solving the problem. Also, the solutions are time and memory-efficient and passed the edge case of when one of the elements of the input array had a <code>0</code> value.</p>
<p>A special mention for <a target="_blank" href="https://www.twitter.com/abiolaEsther_">@abiolaEsther_</a> who implemented a correct time-efficient O(N) <a target="_blank" href="https://gist.github.com/Abiola-Farounbi/3ad0ef587d3c65c584fe2ff1ea6bb5ae">solution</a> and <a target="_blank" href="https://www.twitter.com/toritsejuFO">@toritsejuFO</a> who implemented a correct O(N ^ 2) <a target="_blank" href="https://gist.github.com/toritsejuFO/6641fbb8099c889c97bb628215c9b540">solution</a> in Python using the <code>lambda</code> function. </p>
<p>Thank you once again, to everyone who participated.</p>
<h3 id="conclusion">Conclusion</h3>
<p>I hope reading this article has made you see the importance of asking clarifying questions when developing a solution and also given you examples of the kinds of questions to ask.</p>
<p>Though these questions are by no means an exhaustive list of the kinds of questions you should ask, they show you an example of the things you should be thinking about when you are developing a solution to a problem, whether during a technical interview or in a problem-solving session at your job.</p>
<p>If you are looking for a risk-free, low-stakes opportunity to apply what you learned in this article, you should consider giving <a target="_blank" href="https://meekg33k.dev/introducing-algorithm-fridays">Algorithm Fridays</a> a try. You'll love it.</p>
<p>Thanks for reading this article and see you later today for Week 5 of Algorithm Fridays. Have any questions, feedback or concerns? Please share it in the comment section below. </p>
<p><strong>E go be!</strong></p>
]]></content:encoded></item><item><title><![CDATA[Sorting Algorithms - When Not To Use Them]]></title><description><![CDATA[If you had 30 students in a class and you wanted them to stand in a line; you could decide to arrange the students in line using their names - students whose names start with the alphabet A stay in front of the line while those whose names start with...]]></description><link>https://meekg33k.dev/sorting-algorithms-when-not-to-use-them</link><guid isPermaLink="true">https://meekg33k.dev/sorting-algorithms-when-not-to-use-them</guid><category><![CDATA[sorting]]></category><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[optimization]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Tue, 27 Apr 2021 04:20:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1619434442724/xMml9Xjvo.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you had 30 students in a class and you wanted them to stand in a line; you could decide to arrange the students in line using their names - students whose names start with the alphabet <code>A</code> stay in front of the line while those whose names start with the latter alphabets stay at the rear.</p>
<p>Or, you can decide to arrange them based on their heights; in this case, you would likely bring the shorter students to the front of the line and the taller students to the back of the line.</p>
<p>Both of these methods define some way of arranging the students based on some property. This process is called sorting.</p>
<p>In this article, I will start with a brief introduction about sorting and then highlight briefly the different sorting algorithms and their performance. Then, I will apply that knowledge to the algorithm problem we solved for Week 3 of Algorithm Fridays and in doing so, we will see if using the sort function was the most optimal solution for the problem.</p>
<p>Let's begin with a brief introduction of sorting.</p>
<h3 id="what-is-sorting">What is sorting?</h3>
<blockquote>
<p>Sorting is the process of arranging data in a particular format based on some attribute, for easier data retrieval.</p>
</blockquote>
<p>That's all? Yes that is all.</p>
<p>Using the example of the class students, ordering those students whether by their names or heights is sorting.</p>
<p>Sorting algorithms define the step-by-step approach that describes how the data is sorted. Like with most other algorithms, sorting algorithms help make data more analyzable or easily retrievable.</p>
<p>Now that we know what sorting is and what sorting algorithms are, let's take a look at the different sorting algorithms that are available.</p>
<h3 id="the-different-sorting-algorithms">The Different Sorting algorithms</h3>
<p>There are different <a target="_blank" href="https://www.geeksforgeeks.org/sorting-algorithms/">sorting algorithms</a>  and I want to share this non-exhaustive list of sorting algorithms.</p>
<ul>
<li><a target="_blank" href="https://www.geeksforgeeks.org/insertion-sort/">Insertion sort algorithm</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/bubble-sort/">Bubble sort algorithm</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/quick-sort/">Quick sort algorithm</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/merge-sort/">Merge sort algorithm</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/heap-sort/">Heap sort algorithm</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/radix-sort/">Radix sort algorithm</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/counting-sort/">Counting sort algorithm</a></li>
</ul>
<h4 id="runtime-complexity-of-sorting-algorithms">Runtime complexity of sorting algorithms</h4>
<p>The table below shows the runtime complexity of the different sorting algorithms using <a target="_blank" href="https://www.freecodecamp.org/news/big-o-notation-why-it-matters-and-why-it-doesnt-1674cfa8a23c/">Big O notation</a> :</p>
<table>
<thead>
<tr>
<td>Sort Algorithm</td><td>Worst Case Complexity</td></tr>
</thead>
<tbody>
<tr>
<td>Insertion sort</td><td>O(N<sup>2</sup>)</td></tr>
<tr>
<td>Bubble sort</td><td>O(N<sup>2</sup>)</td></tr>
<tr>
<td>Quick sort</td><td>O(N<sup>2</sup>)</td></tr>
<tr>
<td>Merge sort</td><td>O(N log N)</td></tr>
<tr>
<td>Heap sort</td><td>O(N log N)</td></tr>
<tr>
<td>Radix sort</td><td>O(N)</td></tr>
<tr>
<td>Counting sort</td><td>O(N)</td></tr>
</tbody>
</table>
<p>One important thing to note from here is that the most optimal sorting algorithms have a <a target="_blank" href="https://www.quora.com/What%E2%80%99s-the-simple-explanation-for-O-n-log-n">O(N log N</a> ) runtime complexity.</p>
<p>How do we apply this knowledge in developing a solution to the algorithm problem?</p>
<h3 id="algorithm-problem">Algorithm Problem</h3>
<p>Given the knowledge that we have now of the different kinds of sorting algorithms and their time complexity, let's look at the algorithm problem that more than 20 participants worked on for Week 3 of Algorithm Fridays:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1619404976588/8F9Hm6rbm.png" alt="Algorithm Fridays Week 3 Question.png" /></p>
<h3 id="how-sorting-helps-us-solve-the-problem">How sorting helps us solve the problem</h3>
<p>Looking at the problem definition, a possible first instinct would be to sort the array and then find the first and last occurrences of <code>val</code> in the array. </p>
<p>Here's what that solution (without taking care of edge cases) would look like in JavaScript:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> findFirstAndLastPositionOfVal = <span class="hljs-function">(<span class="hljs-params">nums, val</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> sortedNums = nums.sort(); <span class="hljs-comment">// This makes this function O(N log N)</span>
    <span class="hljs-keyword">const</span> start = nums.indexOf(val);
    <span class="hljs-keyword">const</span> end = nums.lastIndexOf(val);
    <span class="hljs-keyword">return</span> [start, end];
}
</code></pre>
<p>However, knowing that sorting algorithms have a O(N log N) time complexity, the question we should be asking is "<strong>there a way we can solve the problem faster by doing less work?</strong>"</p>
<h3 id="do-we-really-need-to-sort">Do we really need to sort?</h3>
<p>As software engineers developing solutions, it is our place to re-interpret and re-define product requirements to allow us develop the most optimal solutions.</p>
<p>Let's think a bit more about the problem. Why are we sorting the array? </p>
<p>Sorting helps arrange the numbers in increasing order, as that will help us find the positions of <code>val</code> more intuitively.</p>
<p>For instance, if <code>arr = [-2, 3, 0, 7, 11, 3, 3, -19, ]</code> and <code>val = 3</code>, the image below shows us that the array would give us <code>arr = [-19, -2, 0, 3, 3, 3, 7, 11]</code> and we can easily identify the start and end positions of <code>val = 3</code> being <code>[ 3, 5]</code></p>
<blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1619432085017/k9jIPHihA.png" alt="sorted array.png" /> The position of elements in the sorted array</p>
</blockquote>
<p>But do we really need to sort the array to solve this?</p>
<p>Since the goal of sorting is to help order the numbers so that all numbers less than <code>val</code> are placed in front of val, is it possible that we can achieve the same result without doing the 'work' of sorting - again remembering that we want to do the most with the least resources?</p>
<p>What if we focused on finding the numbers that are less than <code>val</code> since we are sure that those numbers would be placed in front of <code>val</code> if we sorted the array? Armed with that information and the number of occurrences of <code>val</code> in the array, we could find the <code>start</code> and <code>end</code> positions of <code>val</code>.</p>
<p>The illustration below will help explain this better:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1619432512060/SbWxOYRj7.png" alt="startAndEndOfVal.png" /></p>
<p>The solution in JavaScript is shown below:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> findStartAndPositionOfVal = <span class="hljs-function">(<span class="hljs-params">nums = [], val</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> val !== <span class="hljs-string">'number'</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Invalid input'</span>);

    <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">Array</span>.isArray(nums) || nums.length == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> [<span class="hljs-number">-1</span>, <span class="hljs-number">-1</span>];

    <span class="hljs-keyword">let</span> countOfNumsLessThanVal = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">let</span> countOfVal = <span class="hljs-number">0</span>

    nums.forEach(<span class="hljs-function">(<span class="hljs-params">num</span>) =&gt;</span> {
      <span class="hljs-keyword">if</span> (num &lt; val) countOfNumsLessThanVal++;
      <span class="hljs-keyword">if</span> (num === val) countOfVal++;
    });

    <span class="hljs-keyword">if</span> (!countOfVal) <span class="hljs-keyword">return</span> [<span class="hljs-number">-1</span>, <span class="hljs-number">-1</span>] <span class="hljs-comment">// val is not found in array</span>

    <span class="hljs-keyword">if</span> (!countOfNumsLessThanVal) {
       <span class="hljs-comment">// Edge case: val is smallest number in array</span>
       <span class="hljs-keyword">return</span> [<span class="hljs-number">0</span>, countOfVal - <span class="hljs-number">1</span>];
    }

    <span class="hljs-keyword">const</span> startOfVal = countOfNumsLessThanVal;
    <span class="hljs-comment">// subtract one because of zero-based indexing</span>
    <span class="hljs-keyword">const</span> endOfVal = startOfVal + countOfVal - <span class="hljs-number">1</span>;

    <span class="hljs-keyword">return</span> [startOfVal, endOfVal ] ;
}

<span class="hljs-comment">/** Test case 1: When nums is null, should return [-1, -1] **/</span>
<span class="hljs-built_in">console</span>.log(findStartAndPositionOfVal (<span class="hljs-literal">null</span>, <span class="hljs-number">5</span>)); <span class="hljs-comment">//✔️</span>

<span class="hljs-comment">/** Test case 2: When nums is an empty array, should return [-1, -1] **/</span>
<span class="hljs-built_in">console</span>.log(findStartAndPositionOfVal ([], <span class="hljs-number">5</span>)); <span class="hljs-comment">// ✔️</span>

<span class="hljs-comment">/** Test case 3: When nums has just one occurrence of val, should return [0, 0]  **/</span>
<span class="hljs-built_in">console</span>.log(findStartAndPositionOfVal([<span class="hljs-number">0</span>], <span class="hljs-number">0</span>)); <span class="hljs-comment">// ✔️</span>

<span class="hljs-comment">/** Test case 4: When nums has multiple occurrences of val,  should return [0, 2] **/</span>
<span class="hljs-built_in">console</span>.log(findStartAndPositionOfVal([<span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>], <span class="hljs-number">3</span>)); <span class="hljs-comment">// ✔️</span>

<span class="hljs-comment">/** Test case 5: When val is the smallest number in nums, should return [0, 2] **/</span>
<span class="hljs-built_in">console</span>.log(findStartAndPositionOfVal([<span class="hljs-number">0</span>, <span class="hljs-number">8</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">12</span>], <span class="hljs-number">0</span>)); <span class="hljs-comment">// ✔️</span>

<span class="hljs-comment">/** Test case 6: When val is the biggest number in nums, should return [5, 5]  **/</span>
<span class="hljs-built_in">console</span>.log(findStartAndPositionOfVal([<span class="hljs-number">0</span>, <span class="hljs-number">-8</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>], <span class="hljs-number">3</span>)); <span class="hljs-comment">// ✔️</span>

<span class="hljs-comment">/** Test case 7: When val is not the smallest nor biggest number in nums, should return [1, 4] **/</span>
<span class="hljs-built_in">console</span>.log(findStartAndPositionOfVal([<span class="hljs-number">0</span>, <span class="hljs-number">-8</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>], <span class="hljs-number">0</span>)); <span class="hljs-comment">//  ✔️</span>
</code></pre>
<p>The much improved solution has a linear, O(N) time complexity, because we only have to iterate through the items in the array just once. If you want to further optimize it, you can use the two-pointer approach I talked about last week in this<a target="_blank" href="https://meekg33k.dev/understanding-how-the-filter-function-works"> article</a>.</p>
<h3 id="winners-of-the-dollar20-award">Winners of the $20 award</h3>
<p>After reviewing all the solutions submitted, we have four optimal solutions for Week 3 of Algorithm Fridays. </p>
<p>The winners are:</p>
<ul>
<li><a target="_blank" href="https://www.twitter.com/freddthink">@freddthink</a>, who was also the winner from Week 2. You can view his solution <a target="_blank" href="https://gist.github.com/Fredpwol/c50dce0f62812819dde3cae775181de2">here</a>.</li>
<li><a target="_blank" href="https://www.twitter.com/chygoz2">@chygoz2</a> and <a target="_blank" href="https://gist.github.com/chygoz2/277e3ba4f04e9b9e82d640dec8174f1d">here</a> is his solution.</li>
<li><a target="_blank" href="https://www.twitter.com/tobi_bams">@tobi_bams</a>, you can view his solution <a target="_blank" href="https://gist.github.com/tobi-bams/b33734e371f7e36757650399e39e3085">here</a>.</li>
<li><a target="_blank" href="https://www.twitter.com/deedee47">@deedee47</a> - our winner from Week 1. Her Java solution can be found <a target="_blank" href="https://gist.github.com/deedee47/52eeee29bcab624a26967b2227f2e403">here</a>.</li>
</ul>
<p>We picked these solutions because apart from being correct and robust, they all were able to solve the problem without having to sort the array - thus achieving a time complexity of O(N) which is better than O(N log N) caused by sorting.</p>
<p>Thank you to everyone who participated in Week 3 of Algorithm Fridays.</p>
<h3 id="conclusion">Conclusion</h3>
<p>Sorting algorithms are powerful and best used when you want to arrange data in a specific format. However like with every other tool available to you, you should only use them when absolutely necessary because it comes with a cost.</p>
<p>Your goal should be to find ways to achieve the same results by doing less computation - less work.</p>
<p>I believe this was worth your read and I hope this challenges you to think a bit more deeply about the code you write when developing solutions.</p>
<p>Please feel free to share your comments or thought on this post or on the <a target="_blank" href="https://twitter.com/meekg33k/status/1385639668440514565">question tweet</a>.</p>
<p>Thanks for reading and see you on Friday for Week 4 of Algorithm Fridays. </p>
<p>Have an amazing week ahead!</p>
<h3 id="further-reading">Further Reading</h3>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/sorting-algorithms-explained-with-examples-in-python-java-and-c/">Sorting Algorithms Explained with Examples in Python, Java, and C++</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/sorting-algorithms/">Sorting Algorithms</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Understanding how the Filter function works]]></title><description><![CDATA[What is a filter function and when would you use a filter function in code?
This was the question that more than 25 participants tried to answer in Week 2's edition of  Algorithm Fridays - a 39-week initiative aimed at promoting community learning ar...]]></description><link>https://meekg33k.dev/understanding-how-the-filter-function-works</link><guid isPermaLink="true">https://meekg33k.dev/understanding-how-the-filter-function-works</guid><category><![CDATA[algorithms]]></category><category><![CDATA[data structures]]></category><category><![CDATA[learning]]></category><category><![CDATA[Developer]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Mon, 19 Apr 2021 13:04:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1618824395971/Gg_7ih6JZ.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>What is a filter function and when would you use a filter function in code?</p>
<p>This was the question that more than 25 participants tried to answer in Week 2's edition of  <a target="_blank" href="https://meekg33k.dev/introducing-algorithm-fridays">Algorithm Fridays</a> - a 39-week initiative aimed at promoting community learning around algorithms and problem-solving.</p>
<p>In this article, I will be explaining how the filter function and using that knowledge, we will see if using the filter function was the most optimal solution for the algorithm problem.</p>
<p>To start off, let's look at the algorithm problem that we worked on for Week 2 of Algorithm Fridays.</p>
<h3 id="algorithm-problem">Algorithm Problem</h3>
<blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1618818020193/Hd_ET6bxg.png" alt="AlgorithmFridays Week 2 Question.png" />
The image above shows the algorithm problem for Week 2 of Algorithm Fridays.</p>
</blockquote>
<p>Giving this problem a bit more thought and drawing from what <a target="_blank" href="https://meekg33k.dev/how-to-find-the-number-of-unique-elements-in-a-sorted-array">we learned last week about doing less work</a> , we can re-interpret this problem to be:</p>
<p> <em>Given an array of numbers, write a function that returns the number of elements in the array <code>nums</code> whose value is not equal to <code>val</code></em>.</p>
<p>The first step to coming up with this solution will be to filter out those numbers in the array whose values are different from <code>val</code>.</p>
<p>What comes to mind when you think of this? That's right - the filter function!</p>
<h3 id="what-is-the-filter-function">What is the filter function?</h3>
<p>Given a list of elements, the filter function returns a subset of the elements in that list that pass a given condition. </p>
<p>For most languages, the filter function has similar signature. It accepts a callback function that tests each element of the array to see if it passes a given condition.</p>
<p>In JavaScript, the signature of the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">filter</a> function looks something like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> newArray = arr.filter(callback(currentValue[, index[, array]]) {
  <span class="hljs-comment">// return element for newArray, if currentValue passes test</span>
}[, thisArg]);
</code></pre>
<p>The filter function is not only common to JavaScript, it also exists in other languages.</p>
<p>While Java also calls it the <a target="_blank" href="https://www.geeksforgeeks.org/stream-filter-java-examples/">filter</a> function, C# refers to it to as the <a target="_blank" href="https://csharp.net-tutorials.com/linq/filtering-data-the-where-method/">Where</a> function while PHP has something similar, called the <a target="_blank" href="https://www.php.net/manual/en/function.array-filter.php">array_filter</a> function.</p>
<h3 id="how-the-filter-function-works">How the filter function works</h3>
<p>Given that many of the solutions for this algorithm problem leveraged the in-built filter function, it is valuable that we look under the hood to understand how the filter function works.</p>
<p>One really important thing to know about the the filter function is that it creates a new array consisting of elements from the original array that match a given condition.</p>
<p>To test this using JavaScript, run the code snippet below:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> originalArray = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
<span class="hljs-keyword">const</span> filteredArray = originalArray.filter(<span class="hljs-function">(<span class="hljs-params">num</span>) =&gt;</span> num % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>)

<span class="hljs-built_in">console</span>.log(originalArray === filteredArray); <span class="hljs-comment">// This would print false because they are not equal</span>
</code></pre>
<p>So, a new array is returned every time the filter function is called because in line with  <a target="_blank" href="https://medium.com/walmartglobaltech/demystifying-functional-programming-part-2-the-need-for-immutability-4f5c16ae2c9a">principles of immutability - a core tenet in functional programming</a>, the filter function doesn't mutate the original (input) array passed to it.</p>
<h3 id="what-does-this-mean-for-the-algorithm-solution">What does this mean for the algorithm solution?</h3>
<p>It means that using the filter function to solve the algorithm problem creates a new array.</p>
<p>While correct, this is not the most memory-optimal solution. In Big O terms, the solution is O(N) where N is the number of filtered elements; elements in the array that aren't equal to the input value, <code>val</code>.</p>
<p>And since our goal is to do the most with the least amount of resources possible, the question we should be asking is, do we really need to create a new array for this problem? </p>
<p>This brings us to our first solution.</p>
<h3 id="solution-1-loop-through-and-maintain-a-count-of-non-equal-elements">Solution 1 - Loop through and maintain a count of non-equal elements</h3>
<p>Since we have re-interpreted this problem as returning the number of elements in the array whose values are not equal to <code>val</code>, there really isn't a need to create a new array.</p>
<p>One solution that avoids creating a new array would entail looping through the elements in the input array and keeping a count of all the elements whose values are not equal to the input value, <code>val</code>.</p>
<p>In JavaScript, the code (without taking care of edge cases) will be something like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> getCountOfNonEqualElements = <span class="hljs-function">(<span class="hljs-params">array = [], val</span>) =&gt;</span> {
   <span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;
   array.forEach(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> {
       <span class="hljs-keyword">if</span> (element !== val) count++;
   });
  <span class="hljs-keyword">return</span> count;
};
</code></pre>
<p>In terms of time complexity, this solution is linear - O(N), because we have to iterate through each element in the array. </p>
<p>Awesome! This works, but can we do better?</p>
<p>If we think about it a bit more, the main computation in our solution is iterating through the items in the input array. Is there a way we can iterate through the array faster?</p>
<p>To answer this question, let's look at what our code currently does.</p>
<h3 id="the-concept-of-two-pointers">The concept of two pointers</h3>
<p>The image below is a graphical illustration of what our code currently does:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1618821909739/Wbs0bfs1I.png" alt="One Pointer Solution.png" /></p>
<p>We have one pointer that moves through the array, pointing to one element at a time and checking to see if that element has a value equal to <code>val</code>.</p>
<p>Okay hold on for one second. Is there a reason why we have just one pointer looping through the array? Is it possible to add another pointer to help us move through the array faster?</p>
<p>Can we implement this solution using two pointers? So that while one starts counting from the beginning of the array, the other counts from the end of the array? Because doing that would halve the time needed to loop through the entire array.</p>
<p>That's exactly what the concept of two pointers is about.</p>
<h3 id="solution-2-use-two-pointers">Solution 2 - Use two pointers</h3>
<p>A second, more optimal solution, will maintain two pointers <code>start</code> and <code>end</code> that will loop through the elements of the input array. </p>
<p>Pointer <code>start</code> will start counting from the beginning of the array while<code>end</code> will count from the end of the array. </p>
<p>Since pointer <code>start</code> starts from 0 and increments towards the end of the array, we have to be careful to make sure that <code>start</code> doesn't end up doing the same work that <code>end</code> is doing. So how do we keep track of that?</p>
<p>Looking at the illustration below, we see that <code>start</code> keeps moving from 0 to the end of the array while <code>end</code> moves from the end of the array to the start.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1618822852572/hyPb6dFVT.png" alt="Two Pointer Solution.png" /></p>
<p>This tells us that as both pointers keep moving, we will get to a point where <code>start</code> and <code>end</code> will either meet (they become equal) and/or they will cross themselves. </p>
<p>This is the point where our loop should end because anything from here on means that either pointer will be duplicating the work the other pointer has already done and we don't want unnecessary work in our code.</p>
<p>The final solution in JavaScript looks something like shown below:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> getCountOfNonEqualElements = <span class="hljs-function">(<span class="hljs-params">nums=[], val</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> countOfNonEqualElements = <span class="hljs-number">0</span>;

   <span class="hljs-comment">// When nums is null or undefined or an empty array</span>
   <span class="hljs-keyword">if</span> (!nums || nums.length === <span class="hljs-number">0</span>) { <span class="hljs-keyword">return</span> countOfNonEqualElements };

   <span class="hljs-keyword">let</span> start = <span class="hljs-number">0</span>;
   <span class="hljs-keyword">let</span> end = nums.length - <span class="hljs-number">1</span>;

   <span class="hljs-keyword">while</span> (start &lt;= end) {
     <span class="hljs-keyword">if</span> (start !== end) {
        <span class="hljs-comment">// When pointers are not equal, </span>
        <span class="hljs-comment">// check for elements at nums[start] and nums[end]</span>
        <span class="hljs-keyword">if</span> (nums[start] !== val) {
           countOfNonValElements++;
        }
       <span class="hljs-keyword">if</span> (nums[end] !== val) {
           countOfNonValElements++;
        }
     }
    <span class="hljs-keyword">else</span> {
       <span class="hljs-comment">// When pointers are equal, no need to duplicate the work </span>
       <span class="hljs-comment">// because nums[start] = nums[end]</span>
       <span class="hljs-keyword">if</span> (nums[end] !== val) {
           countOfNonValElements++;
        }
     }
     start++; <span class="hljs-comment">// Move start pointer forwards</span>
     end--;   <span class="hljs-comment">// Move end pointer backwards</span>
   };
   <span class="hljs-keyword">return</span> countOfNonValElements ;
};
</code></pre>
<h3 id="winner-of-the-dollar20-award">Winner of the $20 award</h3>
<p>After reviewing the 24+ solutions across 4 different programming languages, the $20 award for Week 2 of Algorithm Fridays goes to <a target="_blank" href="https://www.twitter.com/freddthink">@freddthink</a>, and <a target="_blank" href="https://gist.github.com/Fredpwol/9e468f22dc959554f9c3c2603c6f54e7">here</a> is his solution.</p>
<p>We picked this solution because apart from being correct and robust, it is the most optimal of all the solutions with a time complexity of O(N/2).</p>
<p>A special mention to these solutions: <a target="_blank" href="https://www.twitter.com/chygoz2">@chygoz2</a> who implemented a clean O(N) <a target="_blank" href="https://gist.github.com/chygoz2/540ca2dcf141b8340b3194b4eeb5da8b">solution</a> with decent error-handling, <a target="_blank" href="https://www.twitter.com/ibreathcode">@ibreathcode</a> who coded an O(N) <a target="_blank" href="https://gist.github.com/harmlessprince/03bb86d4d73c584728c5ec55c56961ca">solution</a> using PHP, <a target="_blank" href="https://www.twitter.com/_darkmyke">@_darkmyke</a>, <a target="_blank" href="https://www.twitter.com/techgirlamaka">@techgirlamaka</a> and <a target="_blank" href="https://www.twitter.com/tobi_bams">@tobi_bams</a> who all added decent edge-case checks in their solutions and finally, <a target="_blank" href="https://www.twitter.com/TobiAshiru">@TobiAshiru</a> who came up with a O(1) solution in terms of memory usage because he used modified the array  <a target="_blank" href="https://www.interviewcake.com/concept/java/in-place">in-place</a> . </p>
<p>Thank you once again, to everyone who participated.</p>
<h3 id="conclusion">Conclusion</h3>
<p>Even more important than knowing the in-built functions a programming language provides and when to use them, is knowing how these functions work under the hood to help you write optimal code.</p>
<p>I hope this was worth your read and I hope this spurs you on to read more about your programming language's in-built methods.</p>
<p>Please feel free to share your comments or thought on this post or on the  <a target="_blank" href="https://twitter.com/meekg33k/status/1383103731764396033">question tweet</a> .</p>
<p>Thanks for reading and see you on Friday for Week 3 of Algorithm Fridays. Have an amazing week ahead!</p>
]]></content:encoded></item><item><title><![CDATA[How to find the number of unique elements in a sorted array]]></title><description><![CDATA[Introduction
Hello everyone, last Friday was the first of 39 weeks of Algorithm Fridays  - an initiative aimed at promoting community learning around algorithms and problem-solving.
Here is the algorithm problem that we worked on last week:
Algorithm...]]></description><link>https://meekg33k.dev/how-to-find-the-number-of-unique-elements-in-a-sorted-array</link><guid isPermaLink="true">https://meekg33k.dev/how-to-find-the-number-of-unique-elements-in-a-sorted-array</guid><category><![CDATA[algorithms]]></category><category><![CDATA[array]]></category><category><![CDATA[Technical interview]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Mon, 12 Apr 2021 07:32:38 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1618209712306/EJ8yuoqJa.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="introduction">Introduction</h3>
<p>Hello everyone, last Friday was the first of 39 weeks of <a target="_blank" href="https://meekg33k.dev/introducing-algorithm-fridays">Algorithm Fridays</a>  - an initiative aimed at promoting community learning around algorithms and problem-solving.</p>
<p>Here is the algorithm problem that we worked on last week:</p>
<h3 id="algorithm-question">Algorithm Question</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1618154338913/r2eWeww33.png" alt="AlgorithmFridays 9 Apr 2021.png" /></p>
<p>We received 20 solutions for this problem, with some really interesting solutions. Before we look the solutions, let's chat a bit about the problem.</p>
<h3 id="understanding-the-problem">Understanding the Problem</h3>
<p>To solve any problem, it is important that you fully understand the problem while also ensuring that any assumptions you are making about the problem is correct. </p>
<p>One sure way to do that is to ask the right questions.</p>
<h4 id="asking-the-right-questions">Asking the right questions.</h4>
<p>To get a good understanding of the problem, it is important to ask the right questions. For this specific problem, here are some questions worth asking:</p>
<ul>
<li><p>How important is it for the solution to remove the duplicates from the array? Is it sufficient that the solution only returns the number of unique elements in the array, without needing to remove the duplicates? Because if you think about it, that's really what the problem is about. Since the expected result of the function is the number of unique elements in the array, if we can achieve that with less amount of code, the better. Answering this question strongly determines what the final solution will be.</p>
</li>
<li><p>What is the expected input size? How many numbers will be in the input array? This question is important because the input size determines whether to use a data structure or not for the solution. Most entries that were submitted used a Set data structure; the trade-off with that is that most Sets (regardless of language) have a maximum limit of elements that it can store in memory. </p>
</li>
<li><p>What order is the array sorted in? Ascending or descending? Whatever solution we come up with should work regardless of whether the array is sorted in an ascending or a descending order.</p>
</li>
</ul>
<h4 id="thinking-about-edge-cases">Thinking about edge cases</h4>
<p>As software engineers, you always want to avoid the mistake of writing code that only works for the  <a target="_blank" href="https://medium.com/dev-genius/dont-just-test-the-happy-path-e3fd565bad53">happy-path</a>. Your solution should be as robust as is possible and should be able to handle edge cases.</p>
<p>Some edge cases to think about are:</p>
<ul>
<li>What should the expected result be if the input is an empty array?</li>
<li>What should the expected result be if the input is <code>null</code> or <code>undefined</code>?</li>
</ul>
<h3 id="solving-the-problem">Solving the Problem</h3>
<p>Given that there are two ways we can interpret this problem, there are possibly two solutions:</p>
<h4 id="solution-1-removing-the-duplicates-in-the-array">Solution 1: Removing the duplicates in the array</h4>
<p>If we make the focus of the problem to be about removing the duplicates in the array, then one solution would be to use a new data structure to store only the unique elements in this data structure. Most of the solutions submitted used a <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set">Set</a> data structure because the set ensures uniqueness.</p>
<p>One interesting memory-optimal implementation of this was by <a target="_blank" href="https://www.twitter.com/tobiashiru">@tobiashiru</a> who did this in-place without having to create a new array. You can view his solution <a target="_blank" href="https://gist.github.com/KingAshiru/80f4d8682d8c762ec5e73e04ea1c2ff8">here</a>.</p>
<h4 id="solution-2-counting-the-number-of-unique-elements">Solution 2: Counting the number of unique elements</h4>
<p>If we think of this problem as finding the number of unique elements in the array, then our solution focuses on counting the unique number of elements.</p>
<p>The question now is how do we count the number of unique elements?</p>
<h3 id="our-solution">Our Solution</h3>
<p>For a most optimal solution, we elected to go with interpreting the problem as counting the number of unique elements in the input array.</p>
<p>Since the elements are sorted in the array, our solution focuses on counting the number of unique elements by looping through every element in the array and <strong>keeping count of the number of times where we encounter a unique number for the first time</strong>.</p>
<p>But the next question is: "<em>how do we know when we encounter a unique number for the first time?</em>" </p>
<p>There are two scenarios where we possibly encounter a number for the first time:</p>
<ul>
<li>The first element in the array</li>
<li>Whenever there is a change in the value of the element as we loop through the array.</li>
</ul>
<p>The image below shows the times we encounter a unique number for the first time.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1618169397688/F9H0G1hLs.png" alt="Loop through array.png" /></p>
<p>Below is the coded solution in JavaScript using JavaScript's <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce">reduce</a> method:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1618172959222/DnCK92N-7.png" alt="findUniqueEleents.png" /></p>
<p>This solution is most memory-optimal because it doesn't use any extra data structure and is linear - O(N), in terms of time efficiency: linear because we have to loop through every element in the array.</p>
<h3 id="winner-of-the-dollar20-award">Winner of the $20 award</h3>
<p>After reviewing the many different solutions and most of which were correct, the $20 award for Week 1 of Algorithm Fridays goes to <a target="_blank" href="https://twitter.com/DeeGenius47">DeeGenius47</a>, and here is her <a target="_blank" href="https://gist.github.com/deedee47/04801070c8889d863d0b7d393db9e1dd">solution</a>.</p>
<p>We picked this solution because apart from being correct (passed the test cases), it is readable and it is robust - it took care of the edge cases - especially the edge case where the input has a <code>null</code> value, which a lot of the other solutions overlooked.</p>
<p>We also thought to give a special mention to these people: <a target="_blank" href="https://www.twitter.com/nxbstxck">@nxbstxck</a> who added all the different test cases in his <a target="_blank" href="https://gist.github.com/Dev-Nebe/26f499ec34127d0258a488858d07fbb2">solution</a>,  <a target="_blank" href="https://www.twitter.com/franklin_ezeji">@franklin_ezeji</a> whose <a target="_blank" href="https://gist.github.com/Ezeji/16edfbb5f5a01b2a9d3d2d98cebab6bd">solution</a> was memory optimal as it didn't use another data structure and lastly, <a target="_blank" href="https://www.twitter.com/daviddamilola20">@daviddamilola20</a> and <a target="_blank" href="https://www.twitter.com/bytedeveloper_">@bytedeveloper_</a> who both turned in one-liner solutions.</p>
<p>Finally, thanks to everyone who participated!</p>
<h3 id="conclusion">Conclusion</h3>
<p>As software engineers, our work goes beyond just coding up a solution. Whatever solutions we develop should aim to use the least amount of resources as possible - memory, compute etc. </p>
<p>Equally important as developing a solution is understanding exactly what problem we are solving for and more often that not, this entails asking questions about product requirements in a bid to find the most optimal solution. </p>
<p>I hope this was worth your read and you were able to extract some problem-solving principles that can be applied to other problems even in your day-to-day job. Please feel free to share your comments or thought on this post or on the question <a target="_blank" href="https://twitter.com/meekg33k/status/1380567197806964742">tweet</a>.</p>
<p>Thanks for reading and until we meet again this Friday for Week 2's challenge, keep doing more with less resources!</p>
]]></content:encoded></item><item><title><![CDATA[Introducing Algorithm Fridays]]></title><description><![CDATA[Hello there, if you are wondering what Algorithm Fridays is all about, then look no further because you are in the right place. 
A couple of questions before we get started:

What is the first thing that comes to your mind when you hear of the word a...]]></description><link>https://meekg33k.dev/introducing-algorithm-fridays</link><guid isPermaLink="true">https://meekg33k.dev/introducing-algorithm-fridays</guid><category><![CDATA[data structures]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[Technical interview]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Mon, 05 Apr 2021 16:21:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1616855187309/1_vrpVyLO.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello there, if you are wondering what Algorithm Fridays is all about, then look no further because you are in the right place. </p>
<p>A couple of questions before we get started:</p>
<ul>
<li>What is the first thing that comes to your mind when you hear of the word algorithms? </li>
<li>What is the predominant emotion? Fear? Anxiety? Excitement?</li>
</ul>
<p>If you ask a good number of software folks today some of the things they dread most in their careers, even the really good ones too, there's a high chance technical interviewing would be mentioned - possibly next to resolving merge conflicts and writing documentation.</p>
<p>And there are very good reasons for that.</p>
<p>Many would posit that the prevalent technical interviewing process employed by many companies is flawed. Asking questions about arcane and mysterious data structures that may never be used on the job <a target="_blank" href="https://blog.interviewing.io/technical-interview-performance-is-kind-of-arbitrary-heres-the-data/">is hardly a good indication of an engineer's quality</a> and I agree.</p>
<p>However, while I believe that the current technical interviewing process is flawed, I also believe that there is a lot of value in having a good grasp of some computer science fundamentals especially if you are looking to solve problems from <a target="_blank" href="https://fs.blog/2018/04/first-principles/">first-principles</a> . </p>
<p>In my experience, though I have rarely had to use algorithms at work, there have been a few times where <a target="_blank" href="https://meekg33k.dev/3-times-i-used-my-knowledge-of-data-structures-on-the-job">my knowledge of data structures and algorithms were useful on the job</a> and those are some of the most interesting problems I have solved.</p>
<p>Before we talk about what Algorithm Fridays is, it is important to define what algorithms are especially within the context of Algorithm Fridays.</p>
<h3 id="what-is-an-algorithm">What is an Algorithm?</h3>
<p>An algorithm is a set of step-by-step instructions to solve a specific task or problem.</p>
<p>For example, if you are running out of groceries and you need to restock, your step-by-step approach to solving that 'problem' may look something like:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1617470223675/r1kB0avAc.png" alt="Algorithm Flow.png" /></p>
<ol>
<li>Determine what your budget is.</li>
<li>Draw up a list of groceries you can get with the amount from Step 1.</li>
<li>Choose the grocery store and identify how you'd get there (walking, driving, ordering online, etc).</li>
<li>Go (virtually or in person) to the grocery store.</li>
<li>Purchase the groceries.</li>
</ol>
<p>That's your algorithm for solving the problem of grocery re-stocking.</p>
<p>An algorithm within the context of Algorithm Fridays will be no different in concept. It will be a step-by-step set of instructions, written in any programming language, to a specific task or problem.</p>
<h3 id="so-what-then-is-algorithm-fridays">So what then is Algorithm Fridays?</h3>
<p>Algorithm Fridays is a community-focused initiative to help improve your technical problem-solving skills and reward that exercise.</p>
<h3 id="how-will-this-work">How will this work?</h3>
<ul>
<li><p>For 39 Fridays, starting from April 9th up until the last Friday in 2021, I will be posting some byte-sized, not-so-complex algorithm questions on my Twitter handle <a target="_blank" href="https://www.twitter.com/meekg33k">@meekg33k</a> using the <strong>#AlgorithmFridays</strong> hashtag.</p>
</li>
<li><p>You, along with other participants, will solve the algorithm problems in any of their preferred programming language. The goal here is not to test your expertise in a programming language but to improve your problem-solving skills.</p>
</li>
<li><p>Solutions to the algorithm problems will be available the following week in a blog post <a target="_blank" href="https://meekg33k.dev">here</a> on Hashnode.</p>
</li>
<li><p>The questions will be posted at <a target="_blank" href="http://www.timebie.com/std/pst.php?q=10">10.00 am PST</a> every Friday.</p>
</li>
</ul>
<h3 id="what-are-the-objectives">What are the objectives?</h3>
<p>The goal of Algorithm Fridays is to create value in the following ways:</p>
<ul>
<li>Create more awareness about algorithms and technical interviewing.</li>
<li>Deepen knowledge of computer science fundamentals.</li>
<li>Give participants exposure to practical algorithm problems.</li>
<li>Help participants overcome the phobia of algorithms and technical interviews.</li>
<li>Promote learning from one another in the community.</li>
<li>Reward growth including the smallest wins.</li>
</ul>
<h3 id="whats-in-it-for-you">What's in it for you?</h3>
<p>If at the end of this program, you are more comfortable with solving algorithm problems, even if just a little, that's success for us.</p>
<p>In addition to the learning gains, there is a $20 appreciation award every week for one person.</p>
<h3 id="how-will-we-select-the-recipient-of-the-award">How will we select the recipient of the award?</h3>
<p>Since we mentioned that this is not a competition, the $20 award is not a prize. </p>
<p>That said, we will select awardees using the following metrics:</p>
<ul>
<li>Clean code.</li>
<li>Code correctness.</li>
<li>Most optimized solution.</li>
</ul>
<h4 id="clean-code">Clean code</h4>
<p>This includes everything from choosing proper variable and function names, writing modular and easy-to-test code, code that follows <a target="_blank" href="https://stackify.com/solid-design-principles/">SOLID</a> principles etc.</p>
<h4 id="code-correctness">Code correctness</h4>
<p>Your solution will be run against a number of test cases. It should be correct for all kinds of inputs and not fail on boundary conditions or edge cases. </p>
<h4 id="most-optimized-solution">Most optimized solution</h4>
<p>The solution scales well, in terms of memory and time usage, regardless of input size. We will be using  <a target="_blank" href="https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation">Big-O notation</a> to measure performance.</p>
<h3 id="how-will-participants-submit-their-solution">How will participants submit their solution?</h3>
<p>For ease of management and transparency, we ask that solutions be submitted by providing a link to a <a target="_blank" href="https://docs.github.com/en/github/writing-on-github/creating-gists">public GitHub gist</a> via a comment on the question tweet.</p>
<h3 id="what-happens-when-there-are-many-correct-solutions">What happens when there are many correct solutions?</h3>
<p>In the event that there are many different correct solutions to a problem, we will pick awardees randomly. </p>
<p>While we will try to be as fair as possible, we understand that there are limitations with every random selection process and request that participants should accept that.</p>
<h3 id="who-can-participate">Who can participate?</h3>
<p>Participation is by interest and so Algorithm Fridays is open to anybody and everybody.</p>
<h3 id="conclusion">Conclusion</h3>
<p>I hope this is fun and exciting for you, because it is for us and I'm also confident you will benefit a lot from this over the next 39 Fridays.</p>
<p>Let's solve technical problems together.</p>
<p>See you on Friday!</p>
]]></content:encoded></item><item><title><![CDATA[3 times I used my knowledge of data structures on the job]]></title><description><![CDATA[In the second half of 2020, I held numerous free mock technical interviews. During those interviews, one recurring question I often got from the interviewees was: 
"I know I need data structure knowledge to pass most technical interviews, but how muc...]]></description><link>https://meekg33k.dev/3-times-i-used-my-knowledge-of-data-structures-on-the-job</link><guid isPermaLink="true">https://meekg33k.dev/3-times-i-used-my-knowledge-of-data-structures-on-the-job</guid><category><![CDATA[software development]]></category><category><![CDATA[2Articles1Week]]></category><category><![CDATA[data structures]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Tue, 23 Mar 2021 11:45:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1616499562556/4pjC6a6Vw.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the second half of 2020, I held numerous free mock technical interviews. During those interviews, one recurring question I often got from the interviewees was: </p>
<p>"<em>I know I need data structure knowledge to pass most technical interviews, but how much am I going to need it for my day-to-day job</em>?"</p>
<p>My answer to them was mostly: "<em>It depends on what kinds of problems you want to solve</em>".</p>
<p>As engineers starting out get to know, there's no shortage of <a target="_blank" href="https://triplebyte.com/blog/counterpoint-stop-trying-to-force-big-o-into-software-development">companies who use seemingly highfalutin computer science subject matter</a> to vet engineering candidates, even if it's sometimes hard to draw a direct relevance from it to the specific job being interviewed for. </p>
<p>But in this article, I will get into a few examples where I have, in fact, put my data structure knowledge to good use on the job.</p>
<p><strong>TLDR</strong>: I hope I can convince you, in the same way I try with my mock interviewees, that the knowledge of data structures is important – yes for acing that technical interview, but moreso to help you a become a better software engineer in practice.</p>
<p>Let’s jump right in!</p>
<h3 id="optimizing-the-page-load-time-of-a-web-application">Optimizing the page load time of a web application</h3>
<p>I was involved in a project where we were building an in-memory caching layer for a micro-service that served learning content data to users via a web application.</p>
<p>The schema structure for the service started out with three main entities, each identifiable by an unique id, among other metadata: </p>
<ul>
<li>Classes</li>
<li>Subjects</li>
<li>Modules</li>
</ul>
<p>The relationship between the entities was such that a Class could have one or more Subjects (1 to N relationship) and a Subject could have one or more Modules. </p>
<p>A simple entity-relationship diagram showing the relationship between a single Class entity and the other entities would be as shown below:</p>
<p>Given that there are more than one Class entities, what we would have is a forest of trees with the Class entities as the root nodes. Representing this in code using JSON format would be something like the code snippet below:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="1c1c39bb6812639f80f8cc968165c68f"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/1c1c39bb6812639f80f8cc968165c68f" class="embed-card">https://gist.github.com/meekg33k/1c1c39bb6812639f80f8cc968165c68f</a></div><p>Imagine there is a request to get all the modules for a specific Class <code>id</code>. Given this structure, the pseudo-code for that logic would be something like:</p>
<ul>
<li>Iterate through the list of classes to find the Class that matches the Class id.</li>
<li>Look up to find the list of subjects in the Class.</li>
<li>Iterate through the subjects from step 2 and get a list of the modules in each Subject.</li>
<li>Merge the list of modules for each Subject gotten from step 3 into one list.</li>
</ul>
<p>This is an example of a request type that typically slowed down the render time of the web application. It became worse when we added Section entity to Modules and Example entity to Sections – this resulted in a tree-like data structure up to five levels deep!</p>
<p>If we analyze the steps in the pseudo-code above, we can see that using a list data structure for Subjects and Modules isn’t the most optimal as lists are not best suited for search operations.</p>
<p>To optimize this, we did the following:</p>
<ul>
<li>Flattened the hierarchy such that each entity was on the same level as the other. By replacing the tree-like structure with a dictionary that stored all the entities using its id as the key and the entity data as the value, we decreased the lookup time to find an entity.</li>
<li>Create a dictionary of entity categories for faster category-level access. </li>
</ul>
<p>Resulting in a data structure that looked like something below:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="1a6adb6a193b7b0430cb40172eae0f64"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/1a6adb6a193b7b0430cb40172eae0f64" class="embed-card">https://gist.github.com/meekg33k/1a6adb6a193b7b0430cb40172eae0f64</a></div><p>This optimization significantly reduced the page load time as lookup for entity metadata was a lot faster and it allowed us to easily scale by adding more entities.</p>
<h3 id="building-an-auto-suggest-component">Building an auto-suggest component</h3>
<p>If you have ever performed a search for a product to buy on Amazon where as you type in a few characters you begin to see suggestions in a drop-down, then you have used an auto-suggest component.</p>
<p>Building that component was another interesting example for me of where the knowledge of data structures proved to be very valuable on the job.</p>
<p>To build this feature, we opted to use a third-party library. Using the library came with a number of benefits including but not limited to customizable options for the user interface, ease of styling, etc. However, it also meant that we were stuck with whatever the internal implementation was.</p>
<p>That turned out to be our bane. </p>
<p>This library’s implementation of the auto-suggest feature is such that it takes in a list of possible suggestions. When a user types in a character, it would perform a search by iterating through all entries in that list of suggestions to find the suggestions that are prefixed by the characters the user typed.</p>
<p>This worked okay for a small number of suggestions to the list, but as the number increased, we noticed a drop in performance of the component. </p>
<p>To solve this problem, we created a modified component that leveraged a Trie data structure constructed from the suggestion list passed to it. Each word in the list of suggestions is broken into characters, with each node in the Trie corresponding to a single character. </p>
<p>For example, given a list of suggestions made up of the words <code>'nike'</code>, <code>'nil'</code>, <code>'pea'</code>, <code>'eat'</code>, <code>'grind'</code>, <code>'vine'</code>, <code>'take'</code>, '<code>die'</code>, the resulting Trie data structure from the list of words is shown below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616316673646/Yzt46BjdX.png" alt="trie model.png" /></p>
<p>Given this Trie data structure, when a user types the word <code>ni</code>, it returns the suggestions <code>nike</code> and <code>nil</code>. This is because the Trie was designed such that each node in the trie has a list of words whose prefix is made of a sequence of characters in the ancestor nodes and the character in the node.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616316697767/kjRGZvulU.png" alt="trie segment.png" /></p>
<p>For example, the node with character <code>i</code> has <code>nike</code> and <code>nil</code> in its list of words because both words have <code>n</code> (from the ancestor node) and <code>i</code> (from the current node) in its prefix. </p>
<p>Like with all decisions there are trade-offs. </p>
<p>Switching our implementation to leverage a Trie data structure came with a cost. For a suggestion list with less than 1,000 words, using a Trie seemed a bit of an overkill. So the final optimization we did was to only build the Trie data structure when the list of words was more than 1,000.</p>
<h3 id="building-a-queue-system-for-a-bidding-service">Building a queue system for a bidding service</h3>
<p>The last instance where I remember how my knowledge of data structures proved useful in the real world was for a project to build a queuing system for a bidding application. </p>
<p>One of the core functionalities of the application was that users could see a list of items that were up for bidding and place bids for a specific item. Each item up for bidding had a bidding window, a time-frame within which bids should be placed. </p>
<p>If all the bids received for a particular item of the same amount (for example $30 as shown in the image below), the item will go to the user who placed the first bid once the bidding window elapsed.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616316819076/jP17HVzK-.png" alt="queue start.png" /></p>
<p>However, if different bids of varying amounts were placed for an item during the bidding window, the item would go to the highest bidder once the window ends.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616316875700/mc2i6Rl32.png" alt="queue as ideal.png" /></p>
<p>Based on the requirements, the one data structure that seemed ideal for this was the queue. The queue data structure, because of its FIFO principle, easily satisfied the requirement where the item should go to the first bidder if equal bids were received. This is because the first bid that was made during the bidding window would be at the start of the queue. </p>
<p>However, the system needed to take into account that for cases where different bid amounts were received for an item, at any point in time, the highest bidder should always be at the start of the queue. </p>
<p>Basically, it would have to sort the bids based on amount first, and then based on time of creation. These sets of requirements made the <a target="_blank" href="https://www.programiz.com/dsa/priority-queue">priority queue</a> an ideal data structure for this system.</p>
<h3 id="conclusion">Conclusion</h3>
<p>You may not have to use the most arcane data structures every day at your job, but having a sound knowledge of data structures is important. In short, you can think of data structures are a powerful tool in the repertoire of a software engineer to help them come to solutions faster and write cleaner, more efficient code. </p>
<p>So the question I'll leave you with is, "<strong>what kinds of problems are you looking to solve</strong>"?</p>
<p><a target="_blank" href="https://www.urbandictionary.com/define.php?term=E%20go%20be">E go be!</a>  ✌️</p>
<h3 id="further-reading">Further Reading</h3>
<ul>
<li><a target="_blank" href="https://meekg33k.dev/what-you-need-to-know-about-data-structures-to-be-a-better-software-engineer-ckl6x6skb03palis11b4nfz9u">What You Need to Know About Data Structures to be a better Software Engineer</a></li>
<li><a target="_blank" href="https://blog.pragmaticengineer.com/data-structures-and-algorithms-i-actually-used-day-to-day/">Data Structures &amp; Algorithms I Used Working at Tech Companies</a></li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/real-time-application-of-data-structures/">Real-Time Application of Data structures</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[6 Red Flags I Saw While Doing 60+ Technical Interviews in 30 Days]]></title><description><![CDATA[What is the one thing you would look out for if you had to join a company?
Sometime between January and March 2020, I wanted to change jobs and was looking to join a new company. This, among other reasons, led me to embark on a marathon of technical ...]]></description><link>https://meekg33k.dev/6-red-flags-i-saw-while-doing-60-technical-interviews-in-30-days</link><guid isPermaLink="true">https://meekg33k.dev/6-red-flags-i-saw-while-doing-60-technical-interviews-in-30-days</guid><category><![CDATA[interview]]></category><category><![CDATA[Technical interview]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[General Programming]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Thu, 11 Mar 2021 16:49:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1615294282436/WxLI0N-sD.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>What is the one thing you would look out for if you had to join a company?</p>
<p>Sometime between January and March 2020, I wanted to change jobs and was looking to join a new company. This, among other reasons, led me to embark on a <a target="_blank" href="https://meekg33k.dev/what-i-learned-from-doing-60-technical-interviews-in-30-days">marathon of technical interviews – 60+ technical interviews in 30 days</a> . </p>
<p>Doing that many number of interviews in such a short time meant I had an interesting mix of experiences from the various companies I interviewed with, each with their unique culture and values that often reflected in the way their interviews were conducted, intentionally or not.</p>
<p>In this article, I will be sharing some of the red flags I observed while I was on this marathon of technical interviews. I will not be mentioning names of any companies because that’s not the intent behind this article. </p>
<p>The goal of this article is also not to make you paranoid and be on the hunt for red flags in your next interview, far from it. Rather the goal is to equip you with knowledge to help you immediately identify exactly the same or similar red flags in your next interview, and hopefully identifying them will set you up to better handle them. </p>
<p>Even though the stories I’ll be sharing come from my marathon of technical interviews, these red flags do not apply only to technical interviews. They apply to all kinds of interviews and so there’s a lot to learn here for everyone.</p>
<h3 id="the-red-flags">The Red Flags</h3>
<h4 id="1-your-interviewer-is-only-open-to-solving-the-problem-one-way">1. Your interviewer is only open to solving the problem ONE way</h4>
<p>In the world of computing and in life generally, for any given problem, there is typically more than one way to solve that problem. For example, given a sorting problem, you could solve it using a merge-sort algorithm or a heap sort algorithm. </p>
<p>Having this rich number of techniques to solve a problem makes it even more interesting and the general expectation in technical interviews is that you should have the flexibility to solve a problem using your preferred technique.</p>
<p>I had an interview where the interviewer asked me to solve an algorithmic problem. I had started solving the problem using a specific technique when the interviewer stopped me in my tracks and asked that I use another technique. </p>
<p>When I probed a bit further to know why, it appeared that the reason he asked that I used the second wasn’t to test my knowledge of that second technique; it was because he was more ‘comfortable’ with that approach.</p>
<p>It is different if the interviewer wants to test your knowledge of something very specific. For example, given a problem that can be solved using iteration and recursion, the interviewer may want to test your knowledge of recursion and can ask you to solve the problem recursively. That wasn’t the case here.</p>
<p>I ended up using both techniques and discussed the trade-offs but frankly that experience left a bad taste in my mouth especially because that interview was with the hiring manager — my would-be manager, who is someone that can significantly influence your career growth and trajectory. </p>
<h4 id="2-undue-pressure-to-accept-an-offer-letter">2. Undue pressure to accept an offer letter</h4>
<p>It’s very exciting and fulfilling when you go through all preliminary stages of a technical interview, through to the onsite interview (remote or in-person) and then you receive that “<em>Congratulations , we are pleased to offer you…</em>” email.</p>
<p>However, that excitement often becomes short-lived when there is some form of pressure from your soon-to-be employer to accept the offer. It’s a bit more manageable when the pressure comes from someone in HR or the recruiter, but when it’s from the hiring manager, that can be harder to manage.</p>
<p>That was the case for me when I interviewed with a startup based in Palo Alto. They were a small company in terms of staff strength. My onsite interview with them had gone quite well. I had a good conversation with the hiring manager and an even better conversation with the VP of Engineering, so much so that I could tell that I was going to be extended an offer. I asked to know how long I had to accept the offer letter and I was told seventy-two hours.</p>
<p>The offer letter arrived later that evening, and it looked great — a six-figure offer definitely didn’t seem like a bad start. I was also at the final stage of the interview process with other companies too and thankfully, I had enough time to negotiate and accept the offer, or so I thought. </p>
<p>Then the pressure started, incessant calls from the hiring manager and the VP of Engineering, back-to-back emails, all within the allotted time. So much was the pressure that it got to a point where I wasn’t sure I wanted to negotiate the offer anymore. I turned down the offer.</p>
<p>I turned down the offer because the experience got me thinking about the company’s work culture. Were the methods employed by the company to get me to accept the offer indicative of their work culture? If they needed to get something done, how far would they go? </p>
<p>Now don’t get me wrong, yes the company wants to employ you, yes the recruiting team wants to ‘close the deal’ however, it’s very important to pay attention to how the company does this. Do they remain professional about it?</p>
<p>A company’s values go beyond what they say, it shows in what they do and how they do it.</p>
<h4 id="3-not-enough-clarity-about-your-role">3. Not enough clarity about your role</h4>
<p>Among the many reasons why you would join a company is your desire to be involved in valuable work. I had the opportunity to join a US company based in Boulder, Colorado. They had contracted a recruiting agency to help them find someone to fill a Software Engineer position in their firm. </p>
<p>The hiring process started with an exploratory interview with the recruiting agency closely followed by a second interview with a recruiter from the company. In both interviews, I couldn’t get a clear sense of the details of my specific role was — what team I would be on, what kinds of projects I’d be working on, what the career growth pathway was, etc. </p>
<p>I understand that sometimes companies can be going through restructuring, but that didn’t seem to be the case here. It seemed more like the company was focused on completing their headcount. Even though there’s nothing wrong with completing a headcount, I think there is everything wrong with not having a clear purpose for a role for a couple of reasons:</p>
<ul>
<li>It means the role may not be critical to the company’s core business.</li>
<li>If the role isn’t that important, it may mean when a layoff comes, your position may be impacted.</li>
</ul>
<p>On a more personal note, I don’t want to be just a number. I want to work at a place where I have the opportunity to contribute in an impactful way, and I like to believe you would too. So it’s important to get clarity about your role, for where you are today and for future career growth.</p>
<h4 id="4-consistent-lack-of-interest-or-low-morale-from-interviewers">4. Consistent lack of interest or low morale from interviewers</h4>
<p>When looking to join a company, one of the things you simply must care about is the team you will end up working on. At least 25% of your waking hours will be spent interacting with that team whether in-person or virtually.</p>
<p>Interviews offer you an opportunity to experience firsthand what it will look like to work with your prospective teammates, especially since, unless you’re interviewing at a huge company, your interviewers are likely to become your teammates.</p>
<p>If through all the different stages of the interview process, you experience a consistent lack of interest or low morale from your interviewers, you might want to pay attention . </p>
<p>When I experienced that during one of my interviews, I couldn’t exactly tell what the cause was, but I knew something just wasn’t right. After some internal tussle, I decided to trust my gut feelings and ended the interview process with the company. </p>
<p>Fast forward to two months after, two of my interviewers (would-be teammates) had left the company and joined another company (no I wasn’t stalking 🙄, I just checked on LinkedIn ). </p>
<p>Now I’m not saying that during the interview process, there won’t be one or two people, who because of their busy schedules, would have preferred to be doing something else rather than interviewing. Yet, when all of the interviewers don’t want to be there, you certainly want to pay attention to that.</p>
<p>A lack of interest or low morale could be pointers to a combination of any of the following:</p>
<ul>
<li>Your prospective team-mates may be experiencing burnout.</li>
<li>Some internal dissatisfaction with company — culture, policies, something, anything.</li>
<li>The team isn’t that interested in you (hard pill to swallow?), maybe they don’t see you as a long-term hire.</li>
</ul>
<p>Or it could be for reasons that I have not included here, but I implore you to not ignore this red flag if you see it in your next interview.</p>
<h4 id="5-your-interviewers-arent-prepared-for-the-interview">5. Your interviewers aren’t prepared for the interview</h4>
<p>Have you been in an interview before where the interviewer doesn’t seem to have any questions to ask you? Trust me it can get really awkward.</p>
<p>That was my experience during a technical phone-screen interview with an educational technology company based in California. The interviewer wasn’t prepared for our interview and didn’t have any questions at hand. He wasn’t even sure of who he was interviewing and what role I was interviewing for. It wasn’t a pleasant experience.</p>
<p>I understand that there are a myriad of reasons why interviewers may not be prepared for an interview. Some of which include:</p>
<ul>
<li>Lack of proper planning by the HR/recruiting team.</li>
<li>Last-minute changes on the interviewee.</li>
<li>Busy schedules for the interviewer.</li>
<li>The interviewer just wasn’t prepared.</li>
</ul>
<p>I typically won’t act on this red flag in isolation. I will be looking for other red flags in a bid to form a cluster of patterns before making any decision.</p>
<h4 id="6-lack-of-a-clear-direction-on-where-the-company-is-headed">6. Lack of a clear direction on where the company is headed</h4>
<p>It’s fulfilling to be a part of a company that is involved in meaningful work that creates value for its users. Joining such a company would mean you have the desire to contribute  in helping the organization meet its goals. This invariably means the organization must have some goals right?</p>
<p>I was contacted by a startup based in San Francisco via AngelList. I had a first introductory call with a recruiter from the company, closely followed by a phone screen technical interview. </p>
<p>In both interviews, even though the interviewers shared some details about the company, there was a lot of vagueness and about the company’s direction and where the company was headed. </p>
<p>I particularly remember that one question I asked at the time, was about how the company would deal with its growing competition. Sadly, the answers I got didn’t seem convincing and the company later got acquired by the competition.</p>
<p>When you are interviewing to join a company, you are selling more than just your skills, but also yourself — your unique experience. While it’s important to do that, I think it’s equally important that the company should be able to sell you on its vision and what it hopes to achieve.</p>
<p>When I think of joining a company, I picture myself in that company for the next 2–5 years. If my vision for where I want to be in my career doesn’t align with the company’s vision, that is a mismatch that shouldn’t be ignored.</p>
<h2 id="conclusion">Conclusion</h2>
<p>We sometimes focus more on securing the job which is important. But equally important or even more important than getting the job is staying fulfilled on the job. For me, fulfillment meant joining a company that had a clear vision of where they were headed, working in a role that was critical to the company’s business while being equipped with a lot of growth opportunities. </p>
<p>Hopefully, these red flags I have shared will equip you to make better decisions on what companies you choose to grow your career with. I would generally not advise making a decision based on one or two red flags, but if you see a cluster of red flags, you shouldn’t ignore them. I wish you the best in your career journey.</p>
<p>If you ever need someone to do a mock interview with you, feel free to schedule one here or you can reach out directly to me on Twitter <a target="_blank" href="https://www.twitter.com/meekg33k">@meekg33k</a> .</p>
]]></content:encoded></item><item><title><![CDATA[What you need to know about data structures to be a better software engineer]]></title><description><![CDATA[From blood oxygen level sensors on the latest Apple 6 watch series to the latest video upload on social media to user activity tracking in a web application,  data is created at a neck-breaking pace in our world today .
This massive amount of data ge...]]></description><link>https://meekg33k.dev/what-you-need-to-know-about-data-structures-to-be-a-better-software-engineer</link><guid isPermaLink="true">https://meekg33k.dev/what-you-need-to-know-about-data-structures-to-be-a-better-software-engineer</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[software development]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Mon, 15 Feb 2021 18:37:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1613176907657/NHratBd8V.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>From blood oxygen level sensors on the latest Apple 6 watch series to the latest video upload on social media to user activity tracking in a web application,  <a target="_blank" href="https://www.forbes.com/sites/bernardmarr/2018/05/21/how-much-data-do-we-create-every-day-the-mind-blowing-stats-everyone-should-read/#916b01860ba9">data is created at a neck-breaking pace in our world today</a> .</p>
<p>This massive amount of data generated has to flow through software systems from point of creation to the point where they are stored and on to the point of consumption, as efficiently as possible. I often think of software systems as a conduit for passage of data.</p>
<p>It therefore becomes critical that software applications and systems employ very optimal ways of representing data that passes through its system. Little wonder most technical interviews at companies that process massive data test the candidate’s knowledge of data structures.</p>
<p>I held over 58 free mock technical interviews in 2020 and during these interviews, one question I got asked a lot was: How much of data structures does one need to pass an interview and to become a sound software engineer?</p>
<p>Is it sufficient to know just a few basic data structures or do I need to know all data structures including  <a target="_blank" href="https://www.geeksforgeeks.org/red-black-tree-set-1-introduction-2/">red-black trees</a> , tries etc? Which of the many data structures do I need to know? Is there a silver-bullet data structure that can solve all problems or like an MIT Professor once said, “<em>when in doubt, throw in a hash map</em>”?</p>
<p>This article will be my attempt to answer these questions.</p>
<p>I'll start by talking about what data structures are before moving on to what I think are the important things to know about each data structure. Finally, I'll pick a few fundamental data structures and do a deep dive on them especially highlighting their uniqueness and the use cases they are best suited for.</p>
<p>I recognize that some of the ideas in this article are subjective and that there could be a different perspective about this. I'd love to hear your thoughts on this.</p>
<p>Let’s jump right in!</p>
<h3 id="what-are-data-structures">What are data structures?</h3>
<p>Data structures are a way of representing data.</p>
<p>Sorry, did you expect something overly technical? Hang on for a second.</p>
<p>Imagine you have a set of books. How would you arrange them?</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1613177394007/vtian4Ifn.jpeg" alt="image-book-stack.jpeg" /></p>
<p>You could decide to place them on top of each other as shown in the image above.</p>
<p>Or you could place them side-by-side (an array, a list), as in the image shown below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1613177416885/y00AstDtU.jpeg" alt="image-book-list.jpeg" /></p>
<p>Imagine if you were searching for the last book, which of the two layouts would make it easier for you to find the last book?</p>
<p>Hopefully, you said the side-by-side arrangement because you could just go straight to the book whereas, with the first layout, you’d have to remove every book on top of it first before getting to the last book.</p>
<p>So basically if you know you would be searching for books a lot, the side-by-side arrangement seems like a better choice.</p>
<p>However, imagine that the first book is the one you are currently reading and all you care about is that you want to be able to access that book. Then either arrangement would be fine right?</p>
<p>This is what data structures are about really.</p>
<p>A data structure presents a unique layout in memory for storing and organizing data. And just as we’ve seen with the example above, the arrangement of the data in determines the efficiency of certain operations.</p>
<p>Do you still want a more ‘technical’ definition? Alright then,  <a target="_blank" href="https://en.wikipedia.org/wiki/Data_structure">here</a>’s one:</p>
<blockquote>
<p>A data structure is a data organization, management, and storage format that enables efficient access and modification.</p>
</blockquote>
<h3 id="what-data-structures-should-you-know">What data structures should you know?</h3>
<p>I’ll intentionally skip this part and answer this later, so let’s move on.</p>
<p>Okay I’m kidding.</p>
<p>There’s a lot of existing content that tells you what data structures you should know. I’m careful not to add to it because I don’t think anyone should put a cap on how much you can know.</p>
<p>That said, I believe in the  <a target="_blank" href="https://betterexplained.com/articles/understanding-the-pareto-principle-the-8020-rule/">80–20 principle</a>. What 20% of data structures do you need to know to not just excel at a technical interview, but to be a better software engineer at your job?</p>
<p>Here’s what I think that 20% would look:</p>
<ul>
<li>Arrays</li>
<li>Linked Lists</li>
<li>Hash Tables</li>
<li>Trees</li>
<li>Graphs</li>
</ul>
<p>Sorry if that looks like 50 not 20. </p>
<p>The data structures listed above are important because they form the building blocks for a number of more sophisticated data structures. <a target="_blank" href="https://www.educative.io/edpresso/what-is-a-red-black-tree">Red-black trees</a> for example, are first trees, before anything else; and trees are just a bunch of connected linked lists with some defined hierarchy. Similarly, sets use hash tables for their internal implementation.</p>
<p>In addition to knowing these fundamental data structures, <strong>it is crucial that you have an in-depth knowledge of the data structure(s) you use every day at work or at a company you hope to work for</strong>. For example, if you hope to work at a graph database company or on the Google Maps team, having a rich knowledge of graphs and trees would better set you up for success. </p>
<p>With that said, let’s talk about the important details you should know about a data structure.</p>
<h3 id="what-about-a-data-structure-should-you-know">What about a data structure should you know?</h3>
<p>If you had infinite time, infinite mental resources and you love learning, you may want to try to learn <strong>everything</strong> about <strong>all</strong> the data structures. Sadly, infinite time and resources are still a utopian dream.</p>
<p>To utilize our finite resources efficiently, we need to focus on the more important things, the biggest bang for our buck. What then are the important things a good software engineer (and that’s you because you are reading this article) should know about data structures?</p>
<p>Here are the main things you should know about a data structure:</p>
<ul>
<li><strong>Read/Access time</strong>: How long does it take to read from the data structure?</li>
<li><strong>Write time</strong>: How long does it take to update an existing entry in the data structure?</li>
<li><strong>Insert time</strong>: How long does it take to insert a new entry into the data structure?</li>
<li><strong>Delete time</strong>: How long does it take to remove an entry from the data structure?</li>
<li><strong>Search time</strong>: How long does it take to find an entry in the data structure?</li>
</ul>
<p>It is important to note that the times listed above are measured using something called the <a target="_blank" href="https://www.freecodecamp.org/news/big-o-notation-why-it-matters-and-why-it-doesnt-1674cfa8a23c/">Big-O Notation</a>.</p>
<p>One other thing I will add to this list will be: does each record in the data structure have an associated overhead? For example a node entry in a linked list, has more memory footprint than an entry in an array.</p>
<p>Knowing these things help you understand what each data structure is best suited for and making better decisions about when to use which.</p>
<h3 id="some-fundamental-data-structures">Some fundamental data structures</h3>
<p>Now that we know what we should be looking for in a data structure, let us apply this to a few fundamental data structures in a bid to understand their peculiarities and what use cases they are best suited for.</p>
<h4 id="1-arrays">1. Arrays</h4>
<p>An array is a data structure that stores a collection of elements in contiguous memory, with each element identifiable by its index or position.</p>
<p>Contiguous is a big word for saying side-by-side. Arrays use a memory layout that allows elements to be placed side by side (think to our side-by-side book arrangement).</p>
<p>The more important things to know about the array are:</p>
<ul>
<li><strong>Read/Access</strong>: Constant time</li>
<li><strong>Write</strong>: Constant time for fixed-size arrays. Constant <a target="_blank" href="https://medium.com/@satorusasozaki/amortized-time-in-the-time-complexity-of-an-algorithm-6dd9a5d38045">amortized time</a> for dynamic arrays with linear time as worst case.</li>
<li><strong>Insert/Delete</strong>: Linear time because though you can directly access the element to delete using its index, you have to move elements after the target element. </li>
<li><strong>Search</strong>: Linear time - you have to explore each element.</li>
<li><strong>When to use</strong>: You get the gains of an array (constant time read/write) when you have a fixed number of elements to store, e.g. number of alphabets, countries in Asia etc.</li>
<li><strong>When not to use</strong>: Arrays aren't best suited for search operations. They also aren't best for cases with frequent inserts and delete operations because it has to copy all the elements after the index of the inserted/deleted element.</li>
</ul>
<h4 id="2-linked-lists">2. Linked Lists</h4>
<p>A linked list is a linear collection of elements where each element (often called a node) is 'linked' to the next element. Each node stores a reference (something that points it) to the next node, something almost akin to a chain.</p>
<p>Traversing a linked list starts from the first node and uses the reference to the next node, all the way till the final node in the list.</p>
<p>Here are the things to note about the linked list:</p>
<ul>
<li><strong>Read/Access</strong>: Linear time</li>
<li><strong>Write</strong>: Linear time.</li>
<li><strong>Insert/Delete</strong>: Constant time if inserting/deleting at the beginning. Linear otherwise.</li>
<li><strong>Search</strong>: Linear time as you have to explore each node.</li>
<li><strong>When to use</strong>: Linked lists are best suited for cases when you expect frequent insert/delete operations at the top or end of the list. A good example would be stacks and queues.</li>
<li><strong>When not to use</strong>: Don't think linked lists if you want fast search or read operations, except you are reading from the top or the end of the list.</li>
</ul>
<h4 id="3-hash-tables">3. Hash Tables</h4>
<p>A hash table is a data structure that stores data in an associative manner by storing key-value pairs. By using a hashing function for each key, the hash table ensures that no two keys in the hash table are the same.</p>
<p>Sets and hash maps make use of hash tables as their internal implementation. </p>
<p>There’s more to say about hash tables and I will do a deeper dive in my upcoming series on data structures but for now, the key things to know about the hash table are:</p>
<ul>
<li><strong>Read/Access</strong>: Constant time</li>
<li><strong>Write</strong>: Constant time.</li>
<li><strong>Insert/Delete</strong>: Constant time</li>
<li><strong>Search</strong>: Constant time.</li>
<li><strong>When to use</strong>: Always baby, always! But seriously, hash tables have a broad range of use cases because of its constant read, write and insert time.</li>
<li><strong>When not to use</strong>: Hash tables aren’t the best when it comes to iterating over its entries. Also because of its associative nature, each record in a hash table comes with extra memory overhead so use it when you absolutely need the keys. For example, if all you need is constant read/access time for a fixed data, then a simple array is more memory-optimal.</li>
</ul>
<h4 id="4-trees">4. Trees</h4>
<p>A tree is a non-linear data structure that represents the relationship between a root node and zero or more sub-trees in a hierarchical structure.</p>
<p>There are different implementations of the tree data structure including but not limited to: binary trees, binary search trees, red-black trees, AVL trees and n-ary trees. If you will like to learn more about these implementations, I’ve added some links in the resources section.</p>
<p>The time efficiency for the different operations using the tree data structure are:</p>
<ul>
<li><strong>Read/Access</strong>: Best-case: Logarithmic, Worst-case: Linear</li>
<li><strong>Write</strong>: Best-case: Logarithmic, Worst-case: Linear.</li>
<li><strong>Insert/Delete</strong>: Constant time if inserting/deleting at the beginning. Linear otherwise.</li>
<li><strong>Search</strong>: Linear time as you have to explore each node.</li>
<li><strong>When to use</strong>: When you want to represent hierarchical relationships and when you want faster search on sorted data (binary search tree).</li>
<li><strong>When not to use</strong>: Unsuited for searching through unsorted data.</li>
</ul>
<p>As you may have noticed, I have skipped the section on Graphs because graphs are simply a collection of connected trees and so the same things mostly apply.</p>
<h3 id="tldr">TL;DR</h3>
<p>I once worked on a <a target="_blank" href="https://triplebyte.com/blog/3-times-i-used-my-knowledge-of-data-structures-on-the-job">project</a> where a big part of the optimization that needed to be done in reducing the page load time of the app was to change the data structure used for storing a part of the app state from an array to a hash-map. We were carrying out a lot of search operations and an array data structure wasn’t the best for it.</p>
<p>It is therefore important to know the unique features of each data structure and what use cases they are best suited for.</p>
<p>So summarily, </p>
<ul>
<li>For fast lookup operations, a hash-map should be one data structure to consider. </li>
<li>For numeric search operations, think binary search trees.</li>
<li>For character search operations showing relationships between the elements, think tries.</li>
<li>For fast reads and writes, think hash tables and arrays (when the data is of a known size).</li>
<li>For frequent inserts and deletes at the start/end of the data structure, linked lists are your best bet.</li>
<li>To represent relationships between entities, trees would be the most suitable. </li>
<li>To represent directed or undirected non-hierarchical relationships between entities, think graphs.</li>
</ul>
<h3 id="conclusion">Conclusion</h3>
<p>Understanding data structures is not just another item to mark off your checklist for a technical interview. Data structures are a powerful tool in the repertoire of a software engineer to help you write cleaner, more efficient code.</p>
<p>The best engineers know their tools and know when to wield which to solve a specific problem.</p>
<blockquote>
<p>A good tool improves the way you work, a great tool improves the way you think</p>
</blockquote>
<p>Let’s all aim to use great tools today. 😊</p>
<p>If you are interested in a more detailed study on each data structure, I will be starting a series next month where I’ll be doing a deep-dive into the data structures I’ve listed here and a few others. Please subscribe <a target="_blank" href="https://meekg33k.dev">here</a> so you don’t miss out on it.</p>
<p>I'll also be resuming free mock interviews for 2021 by next month, March. So if you ever need to do a free mock technical interview, please feel free to reach out on Twitter <a target="_blank" href="https://www.twitter.com/meekg33k">@meekg33k</a> .</p>
<p>I hope this was worth your time. E go be! ✌️</p>
<h3 id="further-reading">Further Reading</h3>
<ul>
<li><p><a target="_blank" href="https://triplebyte.com/blog/3-times-i-used-my-knowledge-of-data-structures-on-the-job">Tries, dictionaries, and queues: Applying data structure knowledge on the job</a></p>
</li>
<li><p><a target="_blank" href="https://blog.pragmaticengineer.com/data-structures-and-algorithms-i-actually-used-day-to-day/">Data Structures &amp; Algorithms I Used Working at Tech Companies</a></p>
</li>
<li><p><a target="_blank" href="https://adrianmejia.com/most-popular-algorithms-time-complexity-every-programmer-should-know-free-online-tutorial-course/">8 time complexities that every programmer should know</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/all-you-need-to-know-about-tree-data-structures-bceacb85490c/">Everything you need to know about tree data structures</a></p>
</li>
<li><p><a target="_blank" href="http://stackoverflow.com/questions/20170244/why-not-use-hashing-hash-tables-for-everything">When not to use hash tables</a></p>
</li>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/real-time-application-of-data-structures/">Real-time application of Data Structures</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[The Best, Worst, and Most Interesting Moments from my Marathon Month of Technical Interviews]]></title><description><![CDATA[Since I shared my story on the lessons I learned from doing 60+ technical interviews in 30 days, quite a good number of folks have reached out to me on Twitter and on LinkedIn with many questions.
Some of the questions I got include:
“How is it possi...]]></description><link>https://meekg33k.dev/the-best-worst-and-most-interesting-moments-from-my-marathon-month-of-technical-interviews</link><guid isPermaLink="true">https://meekg33k.dev/the-best-worst-and-most-interesting-moments-from-my-marathon-month-of-technical-interviews</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[Technical interview]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Wed, 14 Oct 2020 20:10:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1602461339409/Anb42A28l.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Since I shared my story on the <a target="_blank" href="https://meekg33k.dev/what-i-learned-from-doing-60-technical-interviews-in-30-days-ckda9sn7s00iftss13b0wd0ky">lessons I learned from doing 60+ technical interviews in 30 days</a>, quite a good number of folks have reached out to me on Twitter and on LinkedIn with many questions.</p>
<p>Some of the questions I got include:</p>
<p>“<em>How is it possible to do that many interviews in such a short time?</em>”</p>
<p>“<em>How do you fail one interview and immediately move forward to the next one?</em>”</p>
<p>In that article, I briefly touched on the experiences that formed my motivation for going on that intense run of technical interviews. I also shared lessons I learned from that experience and, thankfully, they’ve turned out to be useful to a lot of people around the world.</p>
<p>What I hadn’t shared yet were specific examples of some of the things I encountered during this period. In this article, I’ll get into some of those details by sharing the most eventful moments during my marathon month of technical interviews – including technical content and personal experiences.</p>
<p>The technical content will focus more on the most challenging algorithmic questions, projects, and coding challenges I faced, while the personal part will be about my experiences — successes, struggles, etc – and the lessons I extracted from these eventful moments. I hope in this you, dear readers, can find something useful to apply to your professional lives.</p>
<h3 id="navigating-the-introductory-phone-call-i-was-least-prepared-for">Navigating the introductory phone call I was least prepared for</h3>
<p>At the time I started interviewing, I had a full-time, non-remote day job as a software engineer with a company in the San Francisco Bay Area. That means I had a busy work schedule, typical of any software engineer.</p>
<p>On one particular day, I had just finished from a technical meeting and got on this call with a recruiter from a certain mid-size technology company based in Palo Alto, California. I joined that call straight out of a meeting and because of that, I didn’t have the time to do proper research on the company – or at least that’s what I told myself.</p>
<p>The phone call turned out to be not so introductory, rather it seemed to be a mini technical interview. Not only was I asked about my experience and some of the projects I had worked on, I was also asked to explain some technical concepts. Some of the concepts I vividly remember include:</p>
<ul>
<li>Closures in JavaScript</li>
<li>Concurrency and lock-mechanisms</li>
<li>Difference between hashing and encryption</li>
</ul>
<p>All in an introductory phone interview? Yes, my friend.</p>
<p>Even though I attempted to answer all of these questions, I wasn’t very coherent in my answers. In addition, the fact that I didn’t do my research on the company before the call didn’t help, as I couldn’t ask a lot of insightful questions about the company and couldn’t also give a good answer when the interviewer asked what I knew about the company.</p>
<p>Some of the lessons I took out of that experience are to never get on a phone call with an interviewer without knowing at least these two things:</p>
<ul>
<li>The first name of your interviewer</li>
<li>At least one tangible thing about the company — what they do, where they are located, any recent news, something</li>
</ul>
<p>The other thing I’ll say is when you are asked questions that you weren’t expecting, try to stay composed and make an attempt at answering them to the best of your knowledge. And if you don’t know about it, it’s okay to say you don’t.</p>
<h3 id="tackling-the-most-challenging-algorithm-question">Tackling the most challenging algorithm question</h3>
<p>Over the course of my interviewing period, I encountered some challenging algorithmic questions ranging from dynamic programming problems to problems where I used <a target="_blank" href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra’s Shortest Path algorithm</a>.</p>
<p>The one algorithmic problem, however, that still stands out to me as the most challenging was a problem that involved constructing words from a 2-D array. Below is the problem statement:</p>
<p><em>Given a 2-D board and a list of words from the dictionary, find all words in the board that are contained in the list. Each word is formed by sequentially adding characters in adjacent cells, whether horizontally or negatively and no character in a cell should be used more than once in a word</em>.</p>
<p>The problem statement written in JavaScript code is seen below:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="b50845ccaf2480c88b2c48a202d0e874"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/b50845ccaf2480c88b2c48a202d0e874" class="embed-card">https://gist.github.com/meekg33k/b50845ccaf2480c88b2c48a202d0e874</a></div><p>My approach in solving this problem is outlined in the steps below:</p>
<ul>
<li>Build a <a target="_blank" href="https://en.wikipedia.org/wiki/Trie">Trie</a> using the words in listOfWords.</li>
<li>Using a combination of recursion and backtracking, explore each cell in the 2-D array starting with the first cell to see if its character matches the first character of any word in the Trie data structure.</li>
</ul>
<p>The image below highlights characters in the matching cells that match the words in the Trie.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1602395957740/W4hJ59pOR.png" alt="listOfWords.png" /></p>
<p>As seen in the image above, the subset of words in listOfWords that can be formed from the characters in the 2-D array are <em>eat, grind,</em> and <em>nike</em>.</p>
<p>For more details about this problem and the Trie data structure, I’ve added some links in the resource section at the end.</p>
<h3 id="getting-a-little-too-ambitious-on-a-take-home-project">Getting a little too ambitious on a take-home project</h3>
<p>I got about 18 take-home “expect-to-spend-5-hours” projects for the period I was on this interviewing marathon. As I mentioned in my first article, one of the companies offered to pay $68/hour to candidates working on their take-home projects. (I wish I could say that was the project I found most interesting.)</p>
<p>The project I found most interesting was one where I was asked to build a font-scraper service. The requirements for this service was such that it would provide an endpoint that accepts a source URL, access the page at that URL, and extract all the fonts on the page set via inline, internal, and external styles. In addition, the service would access every outgoing URL found as a link on the source page, extract fonts from all those pages, and keep doing this recursively.</p>
<p>In addition to the requirements above, the service was meant to be configurable such that the consumer of the service could pass two parameters:</p>
<ul>
<li>A <em>crawlType</em> param that determines how the pages would be traversed using either bread first search or depth first search</li>
<li>A <em>pageLimit</em> param that determines how many pages should be explored. (Interesting stuff!)</li>
</ul>
<p>This project tested everything from knowledge of good system architecture and API design to my decision-making – especially as it concerns structuring a project – and from code readability to code extensibility. It was a very well-thought-out project.</p>
<p>Some of the good things I did on this project were:</p>
<ul>
<li>Unit/Integration tests (more than 700 lines of code with ~90% coverage)</li>
<li>Implemented rate limiting. (This wasn’t part of the requirements but if you’re building APIs, you should be thinking about this)</li>
<li>Implemented some form of queuing system for handling failed scrape jobs. (Again this wasn’t part of the requirements but hey, why not?)</li>
</ul>
<p>The one thing I did wrongly that cost me dearly was I wrote the font-parsing algorithm to extract the CSS from the HTML myself. Because I love to solve problems from first-principles, I was ambitious and wanted to tackle this head-on. Bad move!</p>
<p>It’s important to know when to express your love for challenges; doing that in an interview may not always be the best idea. If you’re going to reinvent the wheel, make sure your wheel is better. I could have just gone with a library that already solved that problem.</p>
<p>For take-home projects, spend time on the core requirements and make sure those work before moving on to extra requirements. I learned that the hard way, you don’t have to.</p>
<h3 id="solving-a-tricky-system-design-question">Solving a Tricky System Design Question</h3>
<p>I was asked to build an interactive shell that allows access to a transactional in-memory key/value store.</p>
<p>The shell would accept the following commands:</p>
<ul>
<li>SET [key] [value]: Sets the given key to the specified value. If the key is already present, overwrite the old value.</li>
<li>GET [key]: Prints out the current value of the specified key. If the key has not been set, it prints a default message.</li>
<li>DELETE [key]: Deletes the given key. If the key has not been set, ignore.</li>
<li>COUNT [value]: Returns the number of keys that have been set to the specified value. If no keys have been set to that value, prints 0.</li>
<li>BEGIN: Starts a transaction. These transactions allow you to modify the state of the system and commit or rollback your changes.</li>
<li>COMMIT: Commits the changes made within the context of the active transaction and ends the active transaction.</li>
<li>ROLLBACK: Throws away changes made within the context of the active transaction and ends the active transaction. If no transaction is active, prints NO TRANSACTION</li>
</ul>
<p>Implementing the SET, GET, DELETE and COUNT commands didn’t seem difficult. The trickiness was in implementing the abstraction that would represent a transaction.</p>
<p>A transaction is created with the BEGIN command and creates a context for the other operations to happen. For example if the following sequence of commands were entered in the interactive shell </p>
<pre><code>&gt; <span class="hljs-keyword">BEGIN</span> //Creates a <span class="hljs-built_in">new</span> <span class="hljs-keyword">transaction</span>
&gt; <span class="hljs-keyword">SET</span> X <span class="hljs-number">5</span>
&gt; <span class="hljs-keyword">SET</span> Y <span class="hljs-number">19</span>
&gt; <span class="hljs-keyword">GET</span> Y
Y = <span class="hljs-number">19</span>
</code></pre><p>The expectation was that a transaction would be created (because of the BEGIN command) and all the following operations would happen and work as they should. However, they would work only in the context of that transaction that was created. This transaction would be the active transaction.</p>
<p>It is also expected that until the active transaction is committed using the COMMIT command, those operations do not persist, and the ROLLBACK command throws away changes made by those operations in the context of the active transaction.</p>
<p>The snippet below provides more clarity about the expected behavior:</p>
<pre><code>&gt; <span class="hljs-keyword">BEGIN</span> //Creates a <span class="hljs-built_in">new</span> <span class="hljs-keyword">transaction</span> which <span class="hljs-keyword">is</span> currently active
&gt; <span class="hljs-keyword">SET</span> X <span class="hljs-number">5</span>
&gt; <span class="hljs-keyword">SET</span> Y <span class="hljs-number">19</span>
&gt; <span class="hljs-keyword">GET</span> Y
Y = <span class="hljs-number">19</span>
&gt; <span class="hljs-keyword">ROLLBACK</span> //Throws away the changes made
&gt; <span class="hljs-keyword">GET</span> Y
Y <span class="hljs-keyword">not</span> <span class="hljs-keyword">set</span> // Changes made <span class="hljs-keyword">by</span> <span class="hljs-keyword">SET</span> Y <span class="hljs-number">19</span> have been discarded
</code></pre><p>The trickier part of this question was understanding that each transaction could spawn a new transaction (think of it as a child transaction).</p>
<p>The newly spawned transaction inherits the context (variables) from its parent transaction and changes made in the context of a child transaction and committed would reflect in the parent transaction because committing wrote directly to the key/value store.</p>
<p>Here is another sequence of operations that explains this better:</p>
<pre><code>&gt; <span class="hljs-keyword">BEGIN</span> //Creates a <span class="hljs-built_in">new</span> <span class="hljs-keyword">transaction</span> which <span class="hljs-keyword">is</span> currently active
&gt; <span class="hljs-keyword">SET</span> X <span class="hljs-number">5</span>
&gt; <span class="hljs-keyword">SET</span> Y <span class="hljs-number">19</span>
&gt; <span class="hljs-keyword">BEGIN</span> //Spawns a <span class="hljs-built_in">new</span> <span class="hljs-keyword">transaction</span> <span class="hljs-keyword">in</span> the context <span class="hljs-keyword">of</span> the previous <span class="hljs-keyword">transaction</span> <span class="hljs-keyword">and</span> now this <span class="hljs-keyword">is</span> currently active
&gt; <span class="hljs-keyword">GET</span> Y
Y = <span class="hljs-number">19</span> //The <span class="hljs-built_in">new</span> <span class="hljs-keyword">transaction</span> has <span class="hljs-keyword">access</span> <span class="hljs-keyword">to</span> the context <span class="hljs-keyword">of</span> its parent <span class="hljs-keyword">transaction</span>
&gt; <span class="hljs-keyword">SET</span> Y <span class="hljs-number">23</span>
&gt; <span class="hljs-keyword">COMMIT</span> //Y<span class="hljs-string">'s new value has been persisted to the key-value store
&gt; GET Y
Y = 23 // Changes made by SET Y 19 have been discarded</span>
</code></pre><p>This question tested everything from understanding of good system design, knowledge of data structures – especially for designing the abstraction that represented a Transaction – and how to write highly performant code.</p>
<h3 id="handling-the-most-hurtful-failure">Handling the most hurtful failure</h3>
<p>Doing 60+ technical interviews in such a short time meant I had a good dose of failures, all coming at a really fast pace. Some I could shake off easily, a few others would hit at the core of my sanity and make me question how good a software engineer I was. Those latter ones were harder to shake off.</p>
<p>The one that turned out to be the most hurtful failure, and I still vividly remember it, came during an interview with a data infrastructure firm headquartered in Denver. The company was looking to fill a senior software engineer position and they reached out to me because they thought I was a good fit.</p>
<p>The introductory call went well, the first technical interview went even better, but it was the second technical interview that went south. I was asked to write a function that took in a list of times and calculated the minimum time difference between different times.</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="69cda2970fd17eb883037ad032c5b6f3"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/69cda2970fd17eb883037ad032c5b6f3" class="embed-card">https://gist.github.com/meekg33k/69cda2970fd17eb883037ad032c5b6f3</a></div><p>One reason why this hurt really bad was because I know I solved the problem correctly, and even the interviewer confirmed it. I had identified the boundary conditions and ensured that the code I wrote handled those conditions and other edge cases. I even went on to ask the interviewer if I should care about cases where the time string was wrongly formatted (12.30 for example vs 12:30). With such a bullseye, then, did I move on to the on-site?</p>
<p>It’s one thing to not pass an interview because you didn’t do well, but to do well and still not pass the interview? I couldn’t wrap my head around that. I remember the day I got that rejection email. I was so devastated that I had to reschedule the remaining interviews I had that day.</p>
<p>After much thought, I had to accept that sometimes the reason you don’t scale an interview may not be you; there could be myriad of other reasons, from a change in company’s hiring plans, an interviewer having a bad day, etc.</p>
<p>That experience taught me that there are things I can control and there are things I can’t. You can control how well you do in the interview, the quality of the code you write, how well you communicate your thought process, etc. One thing you can’t control is the outcome – you can only hope that the things you can control, your actions, can influence the outcome.</p>
<p>So separate yourself from “failed” interviews. When the failures come, seek to take the best lessons out from them and apply it to your next interview. In other words, fail forward.</p>
<h3 id="celebrating-my-biggest-win">Celebrating my biggest win</h3>
<p>Going through 60+ technical interviews in 30 days definitely had more downs than ups. However, when the wins started coming, it was very exciting and fulfilling.</p>
<p>Whether the win was as small as passing a technical phone-screen or a HackerRank coding challenge, or as big as getting to an on-site interview or even the many offers that came, I learned to celebrate every win.</p>
<p>And it was in celebrating all these wins that I discovered the biggest win of all: By going through this exercise, I had grown immensely and I had become more confident in handling technical interviews. That truly was my biggest win!</p>
<p>Sounds like a simple answer, but it’s the truth.</p>
<h3 id="to-you">To you</h3>
<p>You may not embark on a technical interviewing marathon like I did, but most definitely you are facing your own daily challenges as you strive to be better every day. Progress is sure when you’re consistent. Keep plugging away and the wins will come.</p>
<p>I sincerely hope reading about my experiences has been rewarding to you, but more importantly, I hope you have taken away vital lessons that you can apply to your professional and personal lives. I wish you the very best in your career!</p>
<p>I’ll be writing a series on data structures in a few weeks where I’ll be explaining how to know what data structure to use when designing an algorithm. If that’s something you’re interested in, please follow me on  <a target="_blank" href="https://www.twitter.com/meekg33k">Twitter</a>  where I post about my articles.</p>
<p>Also, if you ever need someone to do a mock interview with you, I’ll be more than happy to — free to reach out to me on Twitter @meekg33k.</p>
<p><a target="_blank" href="https://peegin.com/e-go-be-2">E go be</a>  ✌️</p>
<h3 id="helpful-resources">Helpful Resources</h3>
<ul>
<li><a target="_blank" href="https://medium.com/basecs/trying-to-understand-tries-3ec6bede0014">Trying to Understand Tries</a></li>
<li><a target="_blank" href="https://dev.to/techdebtor/get-hired-the-system-design-interview-explained-32j4">Get Hired: The System Design Interview, Explained</a></li>
<li><a target="_blank" href="https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/tutorial/">Breadth First Search Tutorials</a></li>
<li><a target="_blank" href="https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/tutorial/">Depth First Search Tutorials</a> </li>
</ul>
]]></content:encoded></item><item><title><![CDATA[What I Learned from Doing 60+ Technical Interviews in 30 Days]]></title><description><![CDATA[In this article, I’ll share my motivation for doing 60+ technical interviews in 30 days. More importantly, I’ll share 13 lessons I learned from my failures and my successes.
I’ve grouped the lessons into three categories to match the phases of a typi...]]></description><link>https://meekg33k.dev/what-i-learned-from-doing-60-technical-interviews-in-30-days</link><guid isPermaLink="true">https://meekg33k.dev/what-i-learned-from-doing-60-technical-interviews-in-30-days</guid><category><![CDATA[General Programming]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[Interviews]]></category><category><![CDATA[2Articles1Week]]></category><category><![CDATA[data structures]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Fri, 31 Jul 2020 13:36:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1596030012261/pNPY9raPE.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, I’ll share my motivation for doing 60+ technical interviews in 30 days. More importantly, I’ll share 13 lessons I learned from my failures and my successes.</p>
<p>I’ve grouped the lessons into three categories to match the phases of a typical recruitment process.</p>
<p>While most of the lessons apply directly to software engineers and technical professionals, the principles behind these lessons can be applied to all careers. I hope you find something useful that you can apply to your professional lives.</p>
<h2 id="how-did-i-get-started-">How did I get started?</h2>
<blockquote>
<p> “If you’re going to fail, do it fast.” — Unknown</p>
</blockquote>
<p>Like any other software engineer, I’ve had different types of technical interviews - from the dreaded whiteboard coding interview to the unreal 45-minute coding challenge on platforms like HackerRank. While some of my experiences in these interviews were great, others were bad. Really bad.</p>
<p>But I wanted to get really good at interviewing. I wanted to learn to overcome the interviewing phobia and exude confidence at interviews. Like a skilled surfer, I wanted to learn to ride the high pressure waves that came with interviews. I was also looking to change jobs at the time.</p>
<p>So from January through early March 2020, I applied to and was contacted by companies based in the US and Europe. From early-stage startups like Coda to later stage startups like Crunchbase, from mid-size companies like Affirm, to bigger companies like Amazon and even remote companies like Webflow.</p>
<p>109+ applications later, I landed myself more than 60 interviews. These comprised more than 60 introductory phone interviews, 50+ technical phone screen interviews, 18 take-home coding projects, 11 coding challenges and 8 on-site interviews including 3 virtual ones.</p>
<h2 id="what-did-i-learn-">What did I learn?</h2>
<p>For better appreciation, I have grouped the lessons into three categories to match the different phases of a typical recruitment process.</p>
<h3 id="pre-interview-phase">Pre-Interview Phase</h3>
<p>This covers everything from the initial contact with a company to the point where the first interview happens.</p>
<h4 id="1-what-i-learned-about-applications">1. What I learned about applications</h4>
<p>When I started applying to companies, I imagined that the more applications I submitted, the higher my chances of getting an interview would be. Seems logical, huh? So I set a target of 5 applications a day, aiming for 1 interview for every 5 applications.</p>
<p>But my strategy didn’t work as I hoped it would. The number of interview requests I got often fell short of my target. It was almost a 1:12 ratio - 1 interview for every 12 applications.</p>
<p>I was faced with the question: do I need to increase my daily target to, say, 10 companies? Or was there something else I needed to change?</p>
<p>With every unsuccessful application, I saw that something needed to change.</p>
<p>That change came when I took a break from meeting my daily numbers and began to think of my applications differently. I began to see each application as a sales pitch to the hiring manager or whoever was going to be reading my application, but here the product being sold was me.</p>
<p>If a company needed to fill a talent gap and I say I had the skills, I needed to find a way to convince them that I did.</p>
<p>My new task then became to find a way to effectively pitch my unique skills, experience and personality in a way that convinced the hiring manager that I was the right fit for the job.</p>
<p>Here is an example of one of such <em>pitches</em> I came up with:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1596030331456/A8nDIn-ih.png" alt="body image.png"></p>
<p>Backed with my resume, this cover letter had a 95% success rate. The one time this didn’t work, the hiring manager still replied to let me know that the position was no longer available but he would like to connect in the future.</p>
<p>The lesson here is, be very intentional about the application you put forward – quality over quantity. Better still do both. Know your unique competencies and experience and present them in a way that matches the company’s needs without sacrificing your personality.</p>
<p>It is also important to understand the peculiarity of the company you are applying to and its specific needs. A startup or a smaller-sized company may have different needs from a bigger company, thus requiring a different skill-set.</p>
<p>Sell yourself and be sure to back your sales pitch during the interview.</p>
<h4 id="2-what-i-learned-about-recruiter-in-mails">2. What I learned about recruiter in-mails</h4>
<p>During this period, I received a number of in-mails from recruiters (mostly unsolicited) for open roles, the majority of which were roles I wasn’t interested in.</p>
<p>Granted, it was sometimes a lot given my busy schedule but I learned to be empathetic, understanding that these recruiters were only trying to do their jobs.</p>
<p>I stopped seeing these in-mails as noise in my inbox and started making the effort to reply to all recruiter in-mails, even for positions I was not interested in. By doing this, I succeeded in building a network of recruiters that have become a rich resource if I have to switch roles in the future.</p>
<p>Now I don’t expect you may want to start replying to every in-mail you receive. But it might interest you to know that some of the interview requests I got were from recruiters I had replied to before for roles I wasn’t interested in. It never hurts to reply.</p>
<h3 id="the-interview-phase">The Interview Phase</h3>
<p>This covers everything about the interview itself, cutting across the different interview types.</p>
<h4 id="3-how-to-handle-introductory-phone-calls">3. How to handle introductory phone calls</h4>
<p>Yes I get it, you’re busy and many things are competing for your time. But hey, you are also an excellent professional, and that means you never get on a phone call without knowing at least these two things:</p>
<ul>
<li>the first name of your interviewer, and</li>
<li>at least one tangible thing about the company — what they do, where they are located, any recent news, something, anything!</li>
</ul>
<p>I noticed that for interviews where I put in the effort to make these findings, I always came across as being genuinely interested in the company. That’s something recruiters typically look for in these kinds of interviews.</p>
<h4 id="4-how-to-handle-technical-phone-screens">4. How to handle technical phone screens</h4>
<p>The one thing that can almost single-handedly decide how well you do in a technical phone screen interview is your ability to communicate your thoughts clearly.</p>
<p>You may have heard stuff like this before:
“<em>The interviewers care about your thought process. Yes they can see your code but importantly, they want to know why you are doing what you’re doing</em>.”</p>
<p>The interviewer isn’t there with you and so does not have the luxury of seeing other non-verbal cues like your hand gestures or nuances. All the interviewer has is your voice as a means of understanding your thought process.</p>
<p>Now you know how you should lead this conversation, the next question is how do you become good at this? Because the truth is, while expressing your thoughts may come naturally to some people, it doesn’t to others – including me.</p>
<p>So – Practice! Practice!! Practice!!!</p>
<p>Practice doing a lot of mock interviews. Doing these mock interviews with friends made me better and more confident in explaining my thought process. But more interestingly, it helped me develop a new mindset about interviews.</p>
<p>I began to see interviews as a conversation with a friend or a team member. I visualized the interviewer on the other end as one of my friends (I sometimes gave the interviewer a name in my head). So what would have been a high-pressure interview I now saw as a friendly ‘chat’ about a technical problem.</p>
<p>This new mindset, aided by the many practice interviews, helped me grow in confidence so much so that I started enjoying interviews, sorry, technical chats.
How to get started on a problem</p>
<p>Never start solving a problem without fully understanding the problem statement. You are almost never wrong if you start by asking clarifying questions. It’s also a good sign to your interviewer when you ask those questions rather than run with your assumptions.</p>
<h4 id="5-how-to-solve-the-problem">5. How to solve the problem</h4>
<p>Good candidates know how to solve a problem (e.g. a sorting problem), but the best candidates know multiple solutions to a problem and understand the trade-offs of one solution versus the other.</p>
<p>The interviews where I performed the best (Cruise comes to mind) are the ones where I didn’t just solve the algorithmic challenge – I was also able to provide alternative solutions and discuss the trade-offs.</p>
<p>Aim to provide multiple solutions to a problem, be willing to discuss the trade-offs, and be able to implement at least one of them.</p>
<p>For technical interviews, write clean code. Most interviewers care about your code quality as well as the correctness of your solution. Aim for modular code, separate reusable logic into utility functions, name variables and methods properly, and just be a boss!</p>
<h4 id="6-what-to-do-when-you-re-stuck-on-a-problem">6. What to do when you’re stuck on a problem</h4>
<p>There will be times when you’re stuck. And this could be caused by a number of reasons: you don’t have the requisite knowledge, incorrect assumptions, missing details, and so on.</p>
<p>I used to think that at such times I was being judged by how fast I could come up with a solution. So I would be quiet, thinking, not communicating with the interviewer, just thinking.</p>
<p>And this is where a lot of us get it wrong. I get it, you need some alone time to think. But sorry to burst your bubble, that alone time is not when you’re being interviewed by a person.</p>
<p>Yes, your interviewer wants to see that you can come up with a solution, but one thing you must not forget is that they also want to see that <strong>you can collaborate with other team-mates to come up with a solution</strong>. While companies want rock-stars, they also want team-players.</p>
<p>Since your interviewer is a friend, a buddy, a team member who’s on your side and means well for you (Refer to 4), talk to them while you&#39;re figuring it out.</p>
<p>Share your thought process up till the point you got stuck and do it confidently, not like some cry for help. By doing so you just may uncover the solution, as was the case during my interview with Coda.</p>
<h4 id="7-how-to-handle-coding-challenges">7. How to handle coding challenges</h4>
<p>The lessons here apply to interviews that take the form of coding challenges on platforms like Hackerrank, Codility, and so on. Typically these are timed challenges, say 45 minutes or sometimes could be more.</p>
<p>Some of the lessons I shared earlier are useful here, while others like asking clarifying questions don’t apply since there’s no one to ask. So here are some steps I recommend:</p>
<ul>
<li>Read through and fully understand the problem.</li>
<li>Write code that works first, even if it’s a <a target='_blank' rel='noopener noreferrer'  href="https://www.freecodecamp.org/news/brute-force-algorithms-explained/">brute-force algorithm</a>. It may not pass all the test cases but get some working code out there first, hopefully within the first 15–20 minutes.</li>
<li>Test your code with different input types, as this helps you handle edge cases.</li>
<li>Optimize for efficiency (runtime and space complexity).</li>
<li>Repeat Steps 4 and 5, till the very last minute.</li>
</ul>
<p>A good grasp of computer science fundamentals is key here. I’ve added some links to helpful resources in the Resources section below.</p>
<h4 id="8-how-to-handle-take-home-projects">8. How to handle take-home projects</h4>
<p>Take home projects are an opportunity to really shine because you have more time. This also means they can be time-consuming.</p>
<p>One of the companies I interviewed with provided hourly pay, about $68/hr, for the number of hours you worked on their take-home project — it’s that serious, so you should be serious about it. Be sure you really want to be a part of a company before investing your time in the take-home projects.</p>
<p>Never compromise on code quality for take-home projects. Be very intentional about your design decisions, naming conventions, code structure and so on, and be ready to defend your choices.</p>
<h4 id="9-what-tools-should-you-use-">9. What tools should you use?</h4>
<p>During my interview with Course Hero, I used <a target='_blank' rel='noopener noreferrer'  href="https://en.wikipedia.org/wiki/Regular_expression">regex</a> to solve a problem I could have solved using a simpler string parsing algorithm. It turned out to be a bad decision as I didn’t pass the interview.</p>
<p>The lesson: only use tools you’re <strong>very</strong> comfortable with and have <strong>a lot</strong> of experience with.</p>
<h4 id="10-how-to-approach-on-site-interviews">10. How to approach on-site interviews</h4>
<p>Get a good night&#39;s sleep the night before. Arrive early on the day of your interview and smile a lot (it helps portray confidence, but more importantly helps you stay relaxed and be in control).</p>
<p>Confront your fears and accept that even if this doesn’t work out it’s not going to be the end of the world – after all you’re just going to have another technical chat. Then go in and absolutely chat away.</p>
<h4 id="11-how-to-approach-virtual-on-site-interviews">11. How to approach virtual on-site interviews</h4>
<p>These can be very different from on-site interviews because everybody’s eyes are on you – <strong>literally</strong> – and that can be nerve-racking.</p>
<p>I had three virtual on-site interviews and I didn’t pass any of them. Sorry I’m not your guy for this one, but I’ve shared some resources that I think you may find helpful below.</p>
<h3 id="after-the-interview">After the Interview</h3>
<h4 id="12-how-to-handle-failure">12. How to handle failure</h4>
<p>There are many reasons why you didn’t pass an interview. Some of the best engineers I know have failed interviews at some point and still do.</p>
<p>So separate failed interviews from yourself, look for the learning points from each failed interview, and use those to forge ahead. As they say – we move!</p>
<h4 id="13-what-about-success-">13. What about success?</h4>
<p>Celebrate your successes, regardless of how small you think they are.  I have a few ideas for celebration.</p>
<h2 id="am-i-better-after-doing-this-">Am I better after doing this?</h2>
<p>I’m not going to tell you that I&#39;ve aced every interview that has come my way since I embarked on this journey. But assuredly, I can tell you I have gotten better at interviewing and my confidence levels have really grown. And yes, I also got multiple offers too 😊.</p>
<h2 id="what-should-you-do-next-">What should you do next?</h2>
<ul>
<li><p>Practice doing a lot of mock interviews with friends. While I don’t totally agree that practice makes perfect (because perfection sounds like a moving target to me) practice helps you <strong>quickly</strong> identify patterns in interview questions, grow in mastery and ultimately your confidence.</p>
</li>
<li><p>For technical interviews, nothing beats a very good understanding of the fundamentals of data structures and algorithms. I’ve added links to resources I think you may find helpful.</p>
</li>
<li><p>Start interviewing and keep interviewing. Even if you have a job, aim to interview every now and then — maybe once every other month or a quarter. Interviewing is a skill, so keep honing it.</p>
</li>
</ul>
<p>I really hope this was helpful to you. And hopefully some of the lessons shared here will make you more confident and better at interviewing – and will ultimately help you land that job you really want.</p>
<p>If you ever need someone to do a mock interview with you, feel free to reach out to me on Twitter <a target='_blank' rel='noopener noreferrer'  href="https://www.twitter.com/meekg33k">@meekg33k</a>.</p>
<p><a target='_blank' rel='noopener noreferrer'  href="https://peegin.com/e-go-be-2">E go be</a>. ✌️</p>
<h2 id="helpful-resources">Helpful Resources</h2>
<ul>
<li><a target='_blank' rel='noopener noreferrer'  href="https://learntocodewith.me/posts/technical-interview/">The Ultimate Guide to Acing Your Technical Interview | Learn to Code With Me</a> </li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://www.themuse.com/advice/how-to-ace-your-technical-interview">How to Ace Your Technical Interview</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://www.freecodecamp.org/news/the-essential-guide-to-take-home-coding-challenges-a0e746220dd7/">The Essential Guide to Take-home Coding Challenges</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://firstround.com/review/The-anatomy-of-the-perfect-technical-interview-from-a-former-Amazon-VP/">The Anatomy of the Perfect Technical Interview from a Former Amazon VP</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://online.hbs.edu/blog/post/virtual-interview-tips">9 Tips for Mastering Your Next Virtual Interview | HBS Online</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://www.salary.com/passages/8-tips-for-acing-virtual-interviews/3/">8 Skype Interview Tips: Ace Your Virtual Job Interview</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[TypeScript Generics and Type Guards - Explained by Example]]></title><description><![CDATA[TypeScript has some powerful features that enable easy building of scalable JavaScript applications. This article describes how we leveraged two TypeScript features: generics and type guards in writing a robust utility function.
How a utility functio...]]></description><link>https://meekg33k.dev/typescript-generics-and-type-guards-explained-by-example</link><guid isPermaLink="true">https://meekg33k.dev/typescript-generics-and-type-guards-explained-by-example</guid><category><![CDATA[TypeScript]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Tue, 31 Mar 2020 16:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1593265298089/1Se5zx-Tb.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>TypeScript has some powerful features that enable easy building of scalable JavaScript applications. This article describes how we leveraged two TypeScript features: generics and type guards in writing a robust utility function.</p>
<h2 id="how-a-utility-function-was-born">How a utility function was born</h2>
<p>If you had to write logic to check if an object of a certain type is defined and if the object actually has any keys, a simple way to do it in TypeScript would be:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="bb16232ac0730b4469d38d9ce9c74df8"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/bb16232ac0730b4469d38d9ce9c74df8" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/bb16232ac0730b4469d38d9ce9c74df8</a></div><p>If you had to use this logic repeatedly, an ideal thing to do would be to create a function that encapsulates this logic. Written in TypeScript, this function would look something like:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="82c795d98ab87f422030ba72f4b08b3b"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/82c795d98ab87f422030ba72f4b08b3b" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/82c795d98ab87f422030ba72f4b08b3b</a></div><p>Good stuff right? Let’s consume this utility function and see what happens. The code snippet below shows what we see in a VS code editor when we use the utility function.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1593265545147/aQLS_Pi16.png" alt="compilerc-complains.png"></p>
<p>Okay, what just happened? Why are we having the error above?</p>
<p>If you look closely at the code snippet above, the logic inside the function is the same logic we used before we wrote the function but for some reason, TypeScript is complaining. What are we doing wrong?</p>
<h3 id="issue-i-typescript-doesn-t-recognize-the-logic-in-the-utility-function">Issue I: TypeScript doesn’t “recognize” the logic in the utility function</h3>
<p>How do we solve this issue with TypeScript not recognizing the correct logic contained within the <code>isPresentSomeObject</code> utility function?</p>
<p>One way to fix this would be to use the  <a target='_blank' rel='noopener noreferrer'  href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator">non-null assertion operator</a> (!) in TypeScript which is basically you telling the compiler, you know what you are doing.</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="758f37c29452043b12f593c574cf6084"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/758f37c29452043b12f593c574cf6084" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/758f37c29452043b12f593c574cf6084</a></div><p>While this works, I’m not a huge fan of this approach maybe because I’m lazy and don’t want to type the non-null assertion operator (!) every time I have to use this function.</p>
<p>But seriously is there a better, more scalable way we could rewrite this? Hold that thought while we look at what the second issue with this function is.</p>
<h3 id="issue-ii-the-utility-function-does-not-currently-support-other-types">Issue II: The utility function does not currently support other types</h3>
<p>Our utility function works with arguments of <code>SomeObjectType</code> type, what if we had to use the same function with another object of a different type, <code>AnotherObjectType</code>? We could refactor the utility function to support the new type by creating a  <a target='_blank' rel='noopener noreferrer'  href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types">union</a>  of the existing type and the new type, as shown below:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="fbd96d65a1d86beb6f230db2c55c1d9d"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/fbd96d65a1d86beb6f230db2c55c1d9d" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/fbd96d65a1d86beb6f230db2c55c1d9d</a></div><p>Okay, say we have to support a third type and a fourth type? Add the types to the union? Seriously? What if there was a fifth type?</p>
<p>You’ll agree that creating a union of more types is not scalable and at this point, you may catch yourself thinking “<em>Why not just make the argument of type <code>any</code> so it can accept ‘all’ types?</em>”</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="244aa2995bc123de9c2b1595ce13bee4"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/244aa2995bc123de9c2b1595ce13bee4" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/244aa2995bc123de9c2b1595ce13bee4</a></div><p>Err, hang on! The challenge with using type <code>any</code> is that we lose the type safety that comes with TypeScript. So how can we rewrite this function without using type <code>any</code>?</p>
<h2 id="hello-generics">Hello Generics</h2>
<p>According to Wikipedia, <em>Generics are a facility of generic programming that allows you extend a type system to allow “a type or method to operate on objects of various types while providing compile-time type safety”</em>.</p>
<p>Imagine you had an identity function that takes an argument of a certain type and returns the argument. If you had to create one function each for a string, Boolean and number types, your code could possibly look like the code below:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="f0abcf17f4753173ef63bf926d3f15ff"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/f0abcf17f4753173ef63bf926d3f15ff" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/f0abcf17f4753173ef63bf926d3f15ff</a></div><p>Hopefully not because the code snippet above violates the  <a target='_blank' rel='noopener noreferrer'  href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> principle. What we need instead is a way to reuse this logic regardless of argument type. Already seeing the similarity between this and our utility function?</p>
<p>Generics provides that abstraction that allows you define <strong>parameter types</strong> (to be specified later) making it possible to reuse code with different types. With generics, the identity function above becomes:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="bd64c0ba43d2818cca35d0d8e1779ffa"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/bd64c0ba43d2818cca35d0d8e1779ffa" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/bd64c0ba43d2818cca35d0d8e1779ffa</a></div><p>By doing this, we are telling the TypeScript compiler “<em>hey, the argument type is of a parameter type, <strong>T</strong>, and you’ll find out what <strong>T</strong> is at compile time</em>”. This allows us to pass any type as a parameter without losing type-checking.</p>
<p>Rewriting our utility function to use generics, the function becomes:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="88293c983e37c6f0c1c13d12427fdc09"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/88293c983e37c6f0c1c13d12427fdc09" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/88293c983e37c6f0c1c13d12427fdc09</a></div><p>With this, you get both the dynamism that comes with using any type and the safety that comes with type checking: double win, if you ask me.</p>
<p>Challenge 1 solved! Next stop, how do we get the compiler to “recognize” our utility function?</p>
<h2 id="introducing-user-defined-type-guards">Introducing User-Defined Type Guards</h2>
<p>If you’ve used TypeScript for a while but haven’t heard of type guards, you’re in good company. Until we ran into this challenge, I hadn’t heard of them either.</p>
<p>Let’s imagine you have the following defined interfaces:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="9238959e7a9ac8c032636e591589737b"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/9238959e7a9ac8c032636e591589737b" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/9238959e7a9ac8c032636e591589737b</a></div><p>If you had to implement logic that would check for what type an <code>animal</code> is, how would you do it? One way would be to use a  <a target='_blank' rel='noopener noreferrer'  href="https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions">type assertion</a>:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="027a04b6e4ddcca64910940fdb7e0eaa"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/027a04b6e4ddcca64910940fdb7e0eaa" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/027a04b6e4ddcca64910940fdb7e0eaa</a></div><p>Great! What happens if more types are added to the <code>Animal</code> type union?</p>
<p><code>type Animal = Cat | Dog | Goat | Horse | Kangaroo | ...</code></p>
<p>Writing a function for each new type isn’t scalable. How then can we create a utility function that allows the TypeScript compiler infer the type of a variable without having to explicitly use type assertion? This is where type guards come to play.</p>
<p>From the TypeScript <a target='_blank' rel='noopener noreferrer'  href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards">documentation</a>, <em>a type guard is some expression that performs a <strong>runtime check</strong> that <strong>guarantees</strong> the type in <strong>some scope</strong></em>.</p>
<p>What does that even mean?</p>
<p>The gist is that with type guards, you can instruct the TypeScript compiler to infer a specific type for a variable in a certain context.</p>
<p>By using a feature called <strong><em> <a target='_blank' rel='noopener noreferrer'  href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates">type predicates</a> </em></strong>, type guards allow you to tell the TypeScript compiler that the type of an argument is what you say it is. This process of refining types to more specific types is called <em>narrowing</em> and it ensures that the variable type is the exact type you expect at runtime.</p>
<p>Predicates are functions that return Boolean values. Type predicates are of the form <code>parameterName is Type</code> , <code>parameterName</code> being the name of the function argument and <code>Type</code> the argument type, and they also return Boolean values.</p>
<h3 id="how-do-you-define-a-type-guard-">How do you define a type guard?</h3>
<p>To define a type guard, simply define a function whose return type is a <em>type predicate</em>.</p>
<p>So an <code>isCat</code> type guard is a function that has the <code>animal is Cat</code> predicate as its return type as shown below:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="4210896589fcd894e6daec70c472b002"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/4210896589fcd894e6daec70c472b002" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/4210896589fcd894e6daec70c472b002</a></div><p>Note that I didn’t even have to implement any ‘serious’ logic in the function, all I had to do was return <code>true</code> because remember, predicates are functions that return Boolean values.</p>
<p>The <code>isCat</code> type guard tells the type system that we’ve confirmed that the <code>animal</code> argument passed is a <code>Cat</code>, so when the compiler does its  <a target='_blank' rel='noopener noreferrer'  href="https://mariusschulz.com/blog/control-flow-based-type-analysis-in-typescript">control flow analysis</a> and sees our type guard, it says “<em>Oh! Whatever is passed into this function has to be a Cat if it returns true</em>”.</p>
<p>It then narrows the argument type from <code>Animal</code> to <code>Cat</code>, that way, we are guaranteed that what the type we get at runtime will be a structure that is consistent the <code>Cat</code> interface definition.</p>
<p>To further appreciate type guards, let’s use it in an <code>if-else</code> block:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1593268321652/Oiwc5gBv3.png" alt="typeguard-if-else.png"></p>
<p>Remember again that we didn’t implement any ‘serious’ logic in the isCat type guard, but the compiler sees the return type as a <code>Cat</code> because of the <code>animal is Cat</code> predicate; so it doesn’t throw any error in the <code>if</code> block of the code snippet above.</p>
<p>What happens in the <code>else</code> block however is more interesting.</p>
<p>Because the compiler expects a <code>Cat</code> type in the <code>if</code> block, it says whatever is in the <code>else</code> block has got to be a different type — that type is the never type which is a type used for values that  <a target='_blank' rel='noopener noreferrer'  href="https://www.typescriptlang.org/docs/handbook/basic-types.html#never">never</a> occur, hence the error message you see in the <code>else</code> block.</p>
<p>Hopefully that was clear, now let us apply type guards to our utility function and see how that solves the problem we described in the first challenge.</p>
<h3 id="converting-our-utility-function-to-a-type-guard">Converting our utility function to a type guard</h3>
<p>Since a type guard is just a function whose return type is a type predicate, all we need to do is to modify our utility function to return a type predicate.</p>
<p>What type predicate however, do we use for this utility function?</p>
<p>Recall that generics allow you to define parameter types which then get assigned at compile time. To convert our utility function to a type guard, we would take advantage of that and make our return type a type predicate of the form <code>arg is T</code> where <code>T</code> is the argument type.</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="d91d2731125ea9606537b0eb6ee5cd15"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/meekg33k/d91d2731125ea9606537b0eb6ee5cd15" class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" data-card-controls="0" data-card-theme="light">https://gist.github.com/meekg33k/d91d2731125ea9606537b0eb6ee5cd15</a></div><p>And now it works!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1593268563191/1KO_UDXOT.png" alt="typeguardgenericsworks.png"></p>
<h2 id="conclusion">Conclusion</h2>
<p>Thank you for reading. I hope reading this has raised your knowledge level about TypeScript generics and user type guards but more importantly, I hope it has stoked your interest in understanding how the TypeScript compiler works.</p>
<p>If you want to learn more about type guards, the type system or the compiler generally, you may find the links in the Further Reading section helpful.</p>
<p>If you’d like to chat about this or anything at all, I’ll be happy to connect via  <a target='_blank' rel='noopener noreferrer'  href="https://twitter.com/meekg33k">Twitter</a> or  <a target='_blank' rel='noopener noreferrer'  href="https://www.linkedin.com/in/uduak-obong-eren">LinkedIn</a>.</p>
<p>Stay safe 😊.</p>
<h2 id="tl-dr-">TL;DR:</h2>
<ul>
<li>If you catch yourself using type <code>any</code> as a way of making a piece of code more robust, think generics.</li>
<li>Type guards are powerful for narrowing types, satisfying the TypeScript compiler’s control flow process and guaranteeing runtime type-safety.</li>
<li>Think type guards when the type you want to use is not as specific as you’d want or when you want to verify that external data is of a specific type.</li>
<li>There are built-in type guards that come with TypeScript like <code>typeof</code> and <code>instanceof</code> but user-defined type guards can be more powerful when used right.</li>
<li>Like all things powerful, user-defined type guards shouldn’t be abused; they shouldn’t be thought of as a way to hack the TS compiler.</li>
</ul>
<h2 id="further-reading">Further Reading</h2>
<ul>
<li><a target='_blank' rel='noopener noreferrer'  href="http://emmanueltouzery.github.io/blog/posts/2018-04-07-prelude-type-guards.html">Type guards and conditional types in typescript &amp; prelude-ts</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://rangle.io/blog/how-to-use-typescript-type-guards/">How to get the types you want with TypeScript type guards | Rangle.io</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://microsoft.github.io/TypeScript-New-Handbook/chapters/narrowing/">Narrowing</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://mariusschulz.com/blog/control-flow-based-type-analysis-in-typescript">Control Flow Based Type Analysis in TypeScript</a></li>
<li><a target='_blank' rel='noopener noreferrer'  href="https://medium.com/@jackhmwilliams/type-narrowing-in-typescript-44a6757c5a64">Type Narrowing in TypeScript</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[The Observer Design Pattern — An Android Implementation]]></title><description><![CDATA[Hi there, it’s a great pleasure having you here. I’m guessing you are a software engineer and you know about software design patterns, cool! However, if you are like me, you may have asked yourself at some point how to know when to use software desig...]]></description><link>https://meekg33k.dev/the-observer-design-pattern-an-android-implementation</link><guid isPermaLink="true">https://meekg33k.dev/the-observer-design-pattern-an-android-implementation</guid><category><![CDATA[Android]]></category><category><![CDATA[design patterns]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[Mobile Development]]></category><dc:creator><![CDATA[Uduak Obong-Eren]]></dc:creator><pubDate>Fri, 01 Jun 2018 22:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1591996329660/nIpzhsAHa.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi there, it’s a great pleasure having you here. I’m guessing you are a software engineer and you know about software design patterns, cool! However, if you are like me, you may have asked yourself at some point how to know when to use software design patterns and what kind of problems are best solved using software design patterns; I’ll attempt to answer this by sharing my experience.</p>
<p>Recently, I was working on a grocery shopping app and one of the UI screens had the requirement to build an activity showing a list of products such that when a user clicked to add a product to cart, the count of that product would be updated and all other occurrences of that same product elsewhere in the app would also be updated.</p>
<p>Since an image is worth more than a thousand words, let me spare you many words — the image below describes the expected behavior.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1591996384121/t_Uvcb0_H.gif" alt="comfimart.gif"></p>
<p>Thinking through this problem and the interacting pieces, I felt this would be a good case to apply a software design pattern.</p>
<h2 id="introducing-the-observer-design-pattern">Introducing the Observer Design Pattern</h2>
<blockquote>
<p>According to Wikipedia, the observer design pattern is a software design pattern in which an object (called the subject) maintains a list of its dependents (called observers) and notifies them automatically of any state changes, usually by calling one of their methods</p>
</blockquote>
<p>From the definition, the Observer pattern seemed ideal for this problem. This doesn’t mean other patterns wouldn’t work — at least the  <a target='_blank' rel='noopener noreferrer'  href="https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">Publish-Subscribe Design pattern</a>, which is subtly different from the Observer pattern, could also work. If you want to know more on the differences between the two patterns,  <a target='_blank' rel='noopener noreferrer'  href="https://hackernoon.com/observer-vs-pub-sub-pattern-50d3b27f838c">here</a>  is an article by Ahmed shamim hassan, who did a great job in explaining the difference between both patterns.</p>
<p>Now to the more interesting part of this — the application &amp; implementation of this pattern to solve this interesting problem.</p>
<h2 id="identifying-the-key-components">Identifying the key components</h2>
<p>When implementing a software system, it’s important to identify the key components that interact in the system and what roles they play. The key components in an observer pattern are the <em>subject </em> and the <em>observers</em>.</p>
<p>For this specific requirement, the subject is the <code>Cart</code> model and the observers are…? (You want to take a guess?) To appreciate what components constitute the observers, it’s important to understand the expected functionality of this activity screen.</p>
<p>The requirement is such that when a user adds a product to a cart, the count of that product is updated in that current view and the count of every other occurrence of that same product in a view is also updated. So if Product A was listed under the <em>Featured Products</em> section and also listed under <em>Fresh Foods Department</em>, the count of both instances in their respective views would be updated if Product A was added to the cart. This suggests a 1-to-N relationship between product and view as illustrated below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1591996954892/DKdne1mYx.png" alt="Figure 2.png"></p>
<p>I’m guessing by now you’ve been able to figure out what the observers are? That’s right, the product <code>views</code> are the observers.</p>
<h2 id="implementing-the-observer-pattern">Implementing the Observer Pattern</h2>
<p>To implement this pattern, first there had to be a way for the observers to subscribe to changes in the subject (<code>Cart</code>) state. To do this, I created an interface in the <code>Cart</code> class that would be implemented each product <code>view</code>.</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Cart</span> {
    <span class="hljs-comment">/**
     *  Listens for cart state change events
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title">OnStateChangeListener</span> {
        <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">onStateChange</span>(<span class="hljs-params">View v</span>)</span>;
    }
}
</code></pre><p>Each product view simply had to create an instance of this interface (with its implementation) — one listener per product view.</p>
<p>So now we have instantiated these view listeners but they are not listening for changes to the <code>Cart</code> model. To attach listeners to the subject, the <code>Cart</code> class exposes a static <code>setOnStateChangeListener</code> method that takes a reference to the product, a reference to the product view and an instance of the <code>OnStateChangeListener</code> interface as arguments as shown below. This process of attaching listeners to the <code>Cart</code> is how the <code>view</code> subscribes to the subject.</p>
<pre><code><span class="hljs-comment">//view subscribes by attaching listener to Cart state </span>
Cart.setOnStateChangeListener(product, view, mCartListener);
</code></pre><p>The more interesting part is what goes on in the <code>Cart</code> class when a call is made to the <code>setOnStateChangeListener</code> method. Internally, the <code>Cart</code> class maintains two HashMaps -one for Product listeners and the other for View listeners.</p>
<pre><code><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> HashMap&lt;OnStateChangeListener, View&gt; viewListeners = <span class="hljs-keyword">new</span> LinkedHashMap&lt;&gt;();
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> HashMap&lt;Product, ArrayList&lt;OnStateChangeListener&gt;&gt; productListeners = <span class="hljs-keyword">new</span> LinkedHashMap&lt;&gt;();
</code></pre><p>Hang on, hang on! I am guessing you’re wondering why I had to maintain two HashMaps, I’ll explain shortly.</p>
<p>From earlier explanations, we can agree that there is a 1-to-1 relationship between listener and view because each view has its own instance of <code>CartStateListener</code>. The <code>viewListeners</code> HashMap as shown in Fig. 3 below, is a data representation of that listener-view mapping</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1591997517356/2_-_M_OX1.png" alt="Figure 3.png"></p>
<p>The <code>productListeners</code> hash on the other hand maintains a bucket of listeners for each specific product because a single product can be listed in many places (views) in the app and each view has its own listener.</p>
<p>For example, from the illustration below, Product A is listed in three places: ProductView #1, ProductView #2 and ProductView N having listeners viewOneListener, viewThreeListener and viewNListener respectively. These means all three listeners have to be aware of changes to the <code>Cart</code> state that affects Product A.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1591997586224/ULzjDKtW6.png" alt="Figure 4.png"></p>
<p>So it’s safe to say the listeners act as some bridge (in the sense of bridge table) between the two maps, linking the <code>product</code> model and the product <code>view</code>.</p>
<p>Every time there is a change to <code>Cart</code> state either because a product is added to the cart or removed, the Cart model publishes these changes by making a call to some <code>notifyAllListeners(product)</code> method passing the specific product as an argument. This is what happens when that call is made:</p>
<pre><code><span class="hljs-comment">//Get all the listeners for this specific product</span>
ArrayList&lt;OnStateChangeListener&gt; x = productListeners.<span class="hljs-keyword">get</span>(product);

<span class="hljs-comment">//For each listener</span>
<span class="hljs-keyword">for</span> (OnStateChangeListener listener: x){

    <span class="hljs-comment">//Get the view that owns this listener</span>
    View v = viewListeners.<span class="hljs-keyword">get</span>(listener);

    <span class="hljs-comment">//Trigger update on the subscribed view </span>
    listener.onStateChange(v);
}
</code></pre><p>The code below shows what happens in the <code>view</code></p>
<pre><code>mCartListener = <span class="hljs-keyword">new</span> Cart.OnStateChangeListener() {
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onStateChange</span><span class="hljs-params">(View productView)</span> </span>{
        <span class="hljs-comment">//productView gets updated here</span>
    }
};
</code></pre><p>And voilà, it works!</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" data-card-width="600px" data-card-key="2e4d628b39a64b99917c73956a16b477" href="https://giphy.com/gifs/mrw-get-upvote-13hxeOYjoTWtK8" data-card-controls="0" data-card-theme="light">https://giphy.com/gifs/mrw-get-upvote-13hxeOYjoTWtK8</a></div>
<h2 id="possible-optimizations">Possible Optimizations</h2>
<p>Since optimize is the name of the game, we always want to look out for ways we could optimize. One I thought of was adding logic to remove listeners from the HashMaps for products not in the user’s view; that way ensuring that the size of the HashMaps don’t grow considerably.</p>
<h2 id="summary">Summary</h2>
<p>Knowing when to apply a software design pattern to a specific problem first requires a good understanding of the workings of the specific pattern you want to use and an equally good understanding of the problem you are trying to solve but most importantly, understanding if the software pattern is a good fit for the problem.</p>
<p>I hope that wasn’t too long a read (I bet it was…) but I hope even more that it was worth your time. If there’s a better way you would have implemented this and would like to chat about it or you would love to chat anything else over coffee I hope, feel free to reach out on Twitter @meekg33k. </p>
<p>E go be! ✌️✊✌️</p>
]]></content:encoded></item></channel></rss>