<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>algorithms &#8211; Hand-Picked</title>
	<atom:link href="https://hand-picked.io/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>https://hand-picked.io</link>
	<description>Top-notch remote software developers</description>
	<lastBuildDate>Fri, 24 Sep 2021 20:35:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.6.16</generator>

<image>
	<url>https://hand-picked.io/wp-content/uploads/2019/04/cropped-logo_512-2-32x32.png</url>
	<title>algorithms &#8211; Hand-Picked</title>
	<link>https://hand-picked.io</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>A Tale of Two HashMaps, or the importance of reading developer docs</title>
		<link>https://hand-picked.io/tale-of-hashmaps-or-the-importance-of-reading-docs/</link>
					<comments>https://hand-picked.io/tale-of-hashmaps-or-the-importance-of-reading-docs/#respond</comments>
		
		<dc:creator><![CDATA[Julián Álvarez]]></dc:creator>
		<pubDate>Mon, 19 Jul 2021 20:49:28 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[stories]]></category>
		<guid isPermaLink="false">https://hand-picked.io/?p=5968</guid>

					<description><![CDATA[In this article we&#8217;ll show an example of the importance of knowing about Data Structures (HashMaps or Dictionaries in this case) and paying attention to contracts and documentation, with a real story. We had an ads system (picture something like Google Ads) in an e-commerce site. And to make it simple, let&#8217;s say that we [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In this article we&#8217;ll show an example of the importance of knowing about Data Structures (HashMaps or Dictionaries in this case) and paying attention to contracts and documentation, with a real story.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img width="640" height="640" src="https://hand-picked.io/wp-content/uploads/2021/07/blog-media-ads.png" alt="Media Google Ads" class="wp-image-5973" srcset="https://hand-picked.io/wp-content/uploads/2021/07/blog-media-ads.png 640w, https://hand-picked.io/wp-content/uploads/2021/07/blog-media-ads-300x300.png 300w, https://hand-picked.io/wp-content/uploads/2021/07/blog-media-ads-150x150.png 150w" sizes="(max-width: 640px) 100vw, 640px" /><figcaption>Image by <a href="https://pixabay.com/users/megan_rexazin-6742250/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=5000790" target="_blank" rel="noopener">Megan Rexazin</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=5000790" target="_blank" rel="noopener">Pixabay</a></figcaption></figure></div>



<p>We had an ads system (picture something like Google Ads) in an e-commerce site. And to make it simple, let&#8217;s say that we had 3 key endpoints:</p>



<ul><li>One to retrieve ads</li><li>Other to count impressions (a pixel request placed by the retrieved ad)</li><li>The last one to count clicks and do the proper redirect</li></ul>



<p>To count impressions, that endpoint should get an encrypted parameter, decrypt it, verify the (internal, decrypted) parameters with a signature, and then use those values impressions counting.</p>



<p>While the code that generated those links was deployed in servers that were readily updatable, the code that read those links (impressions counter) was deployed in an old monolithic system. Those servers were only deployable every 2 weeks.</p>



<ul><li>Link generator &#8211;&gt; in servers that could update</li><li>Link/impression reader &#8211;&gt; in servers that couldn&#8217;t update</li></ul>



<figure class="wp-block-image size-large"><img width="1024" height="358" src="https://hand-picked.io/wp-content/uploads/2021/07/blog-servers-1024x358.jpeg" alt="Servers Clusters" class="wp-image-5971" srcset="https://hand-picked.io/wp-content/uploads/2021/07/blog-servers-1024x358.jpeg 1024w, https://hand-picked.io/wp-content/uploads/2021/07/blog-servers-300x105.jpeg 300w, https://hand-picked.io/wp-content/uploads/2021/07/blog-servers.jpeg 1280w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Photo by&nbsp;<strong><a href="https://www.pexels.com/@artunchained?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels" target="_blank" rel="noopener">Manuel Geissinger</a></strong>&nbsp;from&nbsp;<strong><a href="https://www.pexels.com/photo/interior-of-office-building-325229/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels" target="_blank" rel="noopener">Pexels</a></strong></figcaption></figure>



<p>When this code was released, both servers (clusters indeed, but let&#8217;s call them servers for simplification) were running JDK 1.5.</p>



<p>In a given moment the server which was part of the readily updatable ones, got updated to JDK 1.6, and at that point.. we lost something…</p>



<p>In the morning we were alerted by the abnormality of having 0 (zero) impressions. Fall that could not be attributed to anything organic, but a code issue.</p>



<p>So, what was happening?</p>



<h2>HashMap implementation changes</h2>



<p>Remember that I said we were creating those links with a big encrypted parameter? That had a signature mechanism that was analyzed by the impression reader server code, and if the signature (simply done by hashing) wasn&#8217;t correct, the impression was discarded.<br>So the code for the impression counter link was something like:</p>



<pre class="wp-block-code"><code>for (String key: parametersMap.keySet()) {
    appendParameter(key, parametersMap.get(key),paramsBuilder); // Appends parameter
    stringToHash += parametersMap.get(key); // append value for the string to hash to get a signature
}

// calculate the hash and add some salt
stringToHash += secret;

signature = calulateHash(stringToHash);

appendParameter("signature", signature, paramsBuilder); // Appends parameter</code></pre>



<p>On the reader/impression counter side:</p>



<pre class="wp-block-code"><code>for (String key: parametersMap.keySet()) { // before this, this code put the parameters into a Map
    stringToHash += parametersMap.get(key);
}

// calculate the hash and add some salt
stringToHash += secret;

signatureToVerify = calulateHash(stringToHash);</code></pre>



<p>Can you spot the error there? It happens that in JDK 1.6, there was a slight change in the hashing code in HashMap, which made another slight change in the order elements were placed in the internal hashtable (an array).</p>



<p>So at the point where the impression counter server did <code>.keySet</code>, it provided those keys/values in an original order (by JDK 1.5) while the new code that generated that, was delivering those values in a different order (by JDK 1.6).</p>



<figure class="wp-block-image size-large"><img width="1024" height="125" src="https://hand-picked.io/wp-content/uploads/2021/07/blog-two-hashmaps-1024x125.png" alt="Hash function comparison" class="wp-image-5970" srcset="https://hand-picked.io/wp-content/uploads/2021/07/blog-two-hashmaps-1024x125.png 1024w, https://hand-picked.io/wp-content/uploads/2021/07/blog-two-hashmaps-300x37.png 300w, https://hand-picked.io/wp-content/uploads/2021/07/blog-two-hashmaps.png 1198w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>How the HashMap#hash() function changed. (But this isn&#8217;t the exact change of this story, we just couldn&#8217;t find it)</figcaption></figure>



<p>Obviously the bug is that relying on that order from the HashMap to append and retrieve values was a violation of the HashMap contract (and Set for instance), where it is expressed that you shouldn&#8217;t rely on the HashMap keys order.</p>



<blockquote class="wp-block-quote"><p>This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.</p><cite>Extracted from the HashMap class Javadoc</cite></blockquote>



<h2>Developer&#8217;s quick solution to save the day</h2>



<p>As the server that counted those impressions wasn&#8217;t available for an update (next code upload was scheduled for the next 2 weeks!), we had to do something on the &#8216;pixel generator&#8217; with that link.</p>



<p>As the counter was expecting those parameters in a given order, we decided to use the old HashMap code, so we could get the same order to verify the signature.</p>



<p>The <code>parametersMap</code>  object ended up being an instance of the <code>HashMap15</code> class, for the sake of letting it sort those elements as the impression counter endpoint expected them to be.</p>



<h2>Cleanup and conclusion</h2>



<p>In a next release we did sort those keys before relying on the order and ditched that temporary class. (It wasn&#8217;t an elegant solution to keep).</p>



<p>So that&#8217;s the story of why we should know about data structures, contracts and reading docs… or maybe about minor bugs that could go unnoticed until big consequences happen. Have you ever had to resort to this kind of tricks to save the day and continue operations?</p>



<p><em>A Tale of Two Cities Book Cover Photo by&nbsp;<strong><a href="https://www.pexels.com/@fotios-photos?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels" target="_blank" rel="noopener">Lisa</a></strong>&nbsp;from&nbsp;<strong><a href="https://www.pexels.com/photo/photo-of-a-tale-of-two-cities-by-charles-dickens-book-2608179/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels" target="_blank" rel="noopener">Pexels</a></strong></em></p>
]]></content:encoded>
					
					<wfw:commentRss>https://hand-picked.io/tale-of-hashmaps-or-the-importance-of-reading-docs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Why do we challenge our candidates with algorithms tests?</title>
		<link>https://hand-picked.io/why-challenge-our-candidates-with-algorithm-tests/</link>
					<comments>https://hand-picked.io/why-challenge-our-candidates-with-algorithm-tests/#respond</comments>
		
		<dc:creator><![CDATA[Julián Álvarez]]></dc:creator>
		<pubDate>Mon, 17 May 2021 20:57:23 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[candidates]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[experts]]></category>
		<category><![CDATA[project]]></category>
		<guid isPermaLink="false">https://hand-picked.io/?p=5899</guid>

					<description><![CDATA[“Why do you ask candidates to do algorithms challenges?” It’s a question that usually happens in our interviews. It may appear that most software development jobs are on a category that won’t require you to use advanced algorithms. Tasks like getting data from a&#160;REST API&#160;and showing them to the user in an accessible way, are [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>“Why do you ask candidates to do algorithms challenges?” It’s a question that usually happens in our interviews. It may appear that most software development jobs are on a category that won’t require you to use advanced algorithms. Tasks like getting data from a&nbsp;<strong>REST API</strong>&nbsp;and showing them to the user in an accessible way, are the most common.</p>



<figure class="wp-block-image size-large"><img width="1024" height="683" src="https://hand-picked.io/wp-content/uploads/2021/05/engineers-whiteboard-apis-1024x683.jpg" alt="Female software engineers in discussion in front of whiteboard" class="wp-image-5910" srcset="https://hand-picked.io/wp-content/uploads/2021/05/engineers-whiteboard-apis-1024x683.jpg 1024w, https://hand-picked.io/wp-content/uploads/2021/05/engineers-whiteboard-apis-300x200.jpg 300w, https://hand-picked.io/wp-content/uploads/2021/05/engineers-whiteboard-apis.jpg 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Photo by <a href="https://unsplash.com/@thisisengineering?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">ThisisEngineering RAEng</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">Unsplash</a></figcaption></figure>



<h2>Algorithms and Data Structures, why?</h2>



<p>So let’s approach this question describing when knowledge of algorithms and data structures kick in -in unexpected ways sometimes- into our jobs.</p>



<p>One of the clearest examples is the usage of indexes in databases. It happens that certain queries start being very slow in systems where load and size have increased. Even optimizing the query (tables order, using less data, etc) would not be enough and the need of creating an index arises. Why indexes? Well, basic understanding of data structures make it easy to understand how data access speed is influenced by the data structures enabling it. The most common index in databases are&nbsp;<strong>B-tree&nbsp;</strong>indexes, which enable searches, access, etc in logarithmmic time. So we have it, algorithms and data structures come along again.</p>



<figure class="wp-block-image size-large"><img width="1024" height="768" src="https://hand-picked.io/wp-content/uploads/2021/05/child-water-1024x768.jpg" alt="child playing with water" class="wp-image-5912" srcset="https://hand-picked.io/wp-content/uploads/2021/05/child-water-1024x768.jpg 1024w, https://hand-picked.io/wp-content/uploads/2021/05/child-water-300x225.jpg 300w, https://hand-picked.io/wp-content/uploads/2021/05/child-water.jpg 1280w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Image by <a href="https://pixabay.com/users/igrow-335413/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=392971" target="_blank" rel="noopener">Mandy Klein</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=392971" target="_blank" rel="noopener">Pixabay</a></figcaption></figure>



<p>There is something called, “<a href="https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/" target="_blank" rel="noreferrer noopener">The Law of leaky abstractions</a>” or “leaky abstractions” as is. Simply said, we could trust that the database itself will take the query and run it in efficiently. As we said before, this happens to be wrong in the long run. Sooner than later, the developer (or DBA?) will have to get his hands dirty and make that query perform better, regardless of semantics.</p>



<p>How does this all DB&nbsp;<strong>SQL</strong>&nbsp;Query thing relates with algorithmic tests? Well, those tests will reveal if the developer is aware of performance impact on many decisions during development. Unnecessary loops, or extra storage used may show disregard on knowing how things work, or that some knowledge is missing.</p>



<p>In some interviews we could find that developers rely too much on a given language libraries (data structures, strings, etc), to the extent that they think the library does magic and they wouldn’t explain “how they would implement it if they had to”. This kind of shallow knowledge gives us developers that, for example, don’t know how sorting works or why allocating memory for arrays in each write access is not good.</p>



<p>Solutions made by aware developers will always have those performance concerns on the radar, which makes those kind of solutions desirable. Clients know that, and in their search for great developers, most of them will want to be sure that the candidate has what it takes.</p>



<figure class="wp-block-image size-large"><img width="640" height="422" src="https://hand-picked.io/wp-content/uploads/2021/05/eniac-programmers.gif" alt="" class="wp-image-5914"/><figcaption><em>ENIAC Programming</em></figcaption></figure>



<p>That’s the reason why at Hand-Picked we do both automated algorithm tests and live coding interviews. We are interested in knowing that our developers will make the cut and have low chances of obvious performance blunders.</p>



<p>I would like to mention some notes about performance in general, there is a saying</p>



<blockquote class="wp-block-quote"><p>Premature optimization is the root of all evil</p><cite>Credited to Donald Knuth, in a longer sentence</cite></blockquote>



<p>which basically states that acting on performance when it isn’t important, does more harm than good. Will you spend time trying out an idea for your customers to use, or rather seeing if you’ll need a Big Data database to store analytics? Above all, even though algorithmic knowledge is valuable, it is more valuable for the developer to have criteria when taking decisions. So there is also other phrase about priorities.</p>



<blockquote class="wp-block-quote"><p>Make it work, Make it nice, Make it fast</p><cite>This formulation of this statement has been attributed to Kent Beck</cite></blockquote>



<p>We can see that making it work (correct, do what it is intended) and ‘nice’ (which means, making maintainable code in some way) come first. What will you make fast if it doesn’t work at all? Rhetoric question, as it may happen that not making something fast enough, may make the thing NOT WORK AT ALL for a given volume, users quantity or data amount. So always pay attention, it may happen that performance could be of utter importance.</p>



<p>These two phrases and that counter-argument may look contradictory but they are put together here just to show how good criteria must always be on top.</p>



<h2>Conclusion</h2>



<p>As performance is an important point in Software Development, <a href="https://hand-picked.io" class="rank-math-link">Hand-Picked</a> verifies the algorithmic knowledge of its candidates through an Automated Coding Platform and a Live Coding interview. This way we know we are dealing with a candidate that has the proper tools to solve performance problems that may arise during their engagements. But we don’t exaggerate it, solving CS Olympics level problems isn’t a requirement to enter the Hand-Picked network, just a good level of criteria when coding.</p>



<p><em>Cover Photo by&nbsp;<a href="https://unsplash.com/@alvarordesign?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noreferrer noopener">Alvaro Reyes</a>&nbsp;on&nbsp;<a href="https://unsplash.com/s/photos/computer-rubik?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noreferrer noopener">Unsplash</a></em></p>
]]></content:encoded>
					
					<wfw:commentRss>https://hand-picked.io/why-challenge-our-candidates-with-algorithm-tests/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
