 So, hello everyone, my name is Prened Ube, I work as a principal engineer at Qlogic Technologies and this talk is about sharing our experiences in competitive programming so far and how we can use those experiences for building applications. So to start, I would like to ask, how many of you available here are aware of at least one of these reasons? All right, almost everyone. So Google Code Jam and Facebook HackerCup, they are one of the most popular coding competitions conducted by Google and Facebook every year and HackerRank and TopCoder, they are one of the most popular websites that conduct these competitions from time to time and I have been using HackerRank for quite some time now. So basically competitive programming is just competing with other programmers to try to solve problems that requires algorithm, data structures and some math, etc. So it includes programs like calculating factorial prime number or much more advanced like implementing a search graph, traversing a tree, etc. So last year I decided to give it a try. I planned that it would train for 6 to 8 months and give a short at Google Code Jam. So, after practicing for some time, I realized that it wasn't as easy as I thought it would be in the beginning and I was still stuck in some of the very basic problems. So, whenever I was stuck in a problem, I would search for any reference solution but most of the time the reference solution was either in C, C++, Java or even in pseudo code but rarely in JavaScript and this is quite frustrating because JavaScript is one of the most popular programming language. So one could expect to have the maximum number of different solutions in it. As it turns out, JavaScript is not very popular when it comes to competitive programming and I have a few statistics to prove my point. So on my left, I have the list of top 10 most popular programming languages according to GitHub for the year 2014 and as accepted, JavaScript shines on the top. On my right, I have the top 10 most popular programming languages according to Google Code Jam for the same year and what's surprising is that not only JavaScript falls back to number 9, also a fairly new language such as Go is quite near to JavaScript in terms of popularity while JavaScript has been there for more than a decade now and if you look at the same statistics for year 2015, JavaScript and Go now shares the exact same rank and this can go even worse in the near future. So I wanted to find out what were the reasons for that and so after doing a bit of research, I was able to drill down all those points into three key areas. So number one is speed. This includes programs like calculating an nth prime number which is quite slower as compared to languages like C and C++. Second is memory. This includes programs like storing a thousand JSON objects which ultimately results in a memory allocation failure and remember this in these competitions we also don't have any database and everything else can be grouped under language limitations which has programs like calculating factorial of a big number let's say thousand which ultimately gives you infinity because it exceeds the JavaScript number limit. So after looking at these limitations, I had either two choices either to switch to languages like C and C++ who already have proven solutions for all the problems we mentioned or to keep using JavaScript and keep trying to find ways and hope if we are lucky enough that we'll be able to solve it. So after trying for some time, I finally got some workarounds which helped me to solve all these problems. So before we'll look at some of those workarounds, let's see why you should care about competitive programming. So if you're not interested in competitive programming and you do not write programs or you do not build web applications that requires those sophisticated optimized algorithm, why you should care? So there are mainly two reasons for that. First one is speed and second in memory. So the problem statements given in these competitions, they demand fast execution and they demand efficient memory usage apart from the results being correct. JavaScript nowadays it's not just in your browser. It's also running your server. So you have to write code that executes faster to improve the server response time. And it's also in mobile applications. So you have to write memory efficient solutions because of the memory limitations in these devices. So practicing in these competitions provides us knowledge that empowers us to write code that has a fast execution simply because now we write optimized algorithm that executes faster. It has a much more efficient memory usage since there is no database in these competitions. You have to write programs that execute everything in memory. We get innovative ideas to solve problems. This is one of the most important benefit we get from these competitions. So the problem statements are so complex and so tricky that the usual implementations will just fail. So you have to come up with the new ideas. We get improved server response time. That's because we now have a fast execution. And we get improved overall app performance. But ultimately we get a happy user. So if you ignore all the above points that we just talked about, having a happy user should be the ultimate goal for any application developer. So without wasting time, let's have a look at a practical example. So to keep things more realistic, I'll be actually exiting my code in a competitive program. So I'll be using Hackerang. So here I have a simple factorial example. And if you look at the code, it's quite simple. I'm using a recursive function. And let's try to validate. So let's try to run it for a number, let's say five. So 120 is right. Let's try for one more number. This seems fine as well. So what I'll do is I'll submit my code. So as soon as I submit my code, what these competitions do is they run your programs against a series of hidden test cases. And that's the standard process followed by all the other competitions. Okay. So as we can see that out of 11 test cases, only four of them executed successfully and rest everything else failed. Let's see what went wrong. So what I can do is since it is a practice program, I can download a test case and see what failed. So let's see what was the input. So this was the input number 88. And let's see what was the expected output. So let's see what was the expected output. So this was the output, which is a very big number. Now let's execute the same number in our program. Let's see what we are getting. As we can see that as soon as the number becomes large enough, your language will drain down to a scientific notation, which is what we don't want. We don't want all the digits to be represented to the largest of the number. Let's try for even bigger number. Let's say 200. It gives you infinity because as soon as you try to calculate factorial of any number, that's about 170. It will exceed the JavaScript number limit and you'll get infinity. Let's see how we can fix this problem now. This is the way we execute a multiplication command in our system. We have two operands and we have one operator. But this is not the way we were taught multiplication in school. The way we were taught multiplication in school was just using a pen and paper. And the algorithm we used for that was this. We start with the rightmost digit of the first number. We multiply it with the rightmost digit of the second number. If we get a digit, which is a double digit, we carry forward one digit and we keep the other one. And we keep doing that for the second number. And we put x. And we start with the second number of the first digit, which is 1. We multiply it with the rightmost digit of the second number. And we keep doing the same till we exhaust all the digits of both the numbers. And once we are done, we add them. And this is what we get. And this is the product of both of these value. So this algorithm was simple enough for a human brain to understand because we only multiply two single digits number at a time. So I thought, what if we can use the same algorithm to calculate factorial? Because the main part that's troubling us was the number limit. But if we use the same algorithm, we'll eventually reach infinity and to scientific notation. But if we convert them to strings, it makes sense. And in JavaScript, it's perfectly legal to multiply two strings if they can be implicitly type converted back to number. So using this algorithm, the maximum operands they'll ever have is 9 cross 9. And the maximum value that we'll ever get is 81, which is quite comfortably under the JavaScript number limit. And similarly, these are the operations that we can perform. And I think scientific calculations may be possible. I never tried that. But I think that's maybe possible. Let's try the same problem, but with the improvised algorithm this time. And I'll try to run my code for number 5. It seems fine. Let's try for a bigger number for which our test case failed, which was 88. And we do get all the values. Let's try for even bigger number, let's say 200. And we don't get infinity this time. Let's submit our code. So now we are able to execute all the test cases. Let's try a real world example. So that was fine for calculating a factorial. But what about the benefits we get? So we were building a health care application for one of our client. And that application allowed the patients and the doctor using that to have a video consult between them. So in order for a patient to book a video consult, a patient has to see the availability of a doctor. And to record that availability, we had a calendar. So this is a fairly simple calendar where we used to record all the available times of our doctor. And so if a doctor is available for a given or slot, let's say 1 PM to 2 PM, we mark that area as green. And if a doctor has a multiple availability, we mark those areas green as well. And we keep doing that till we record availability for the entire week. So this was not just a view for a single doctor. This was a summarized view where we used to show availability for all the doctors in our system. So the advantage of this view was an admin using this view can find out areas where there isn't any doctor available and try to fill in the gap. This was the JSON document we used. We had a doctor reference. We had a date reference. And we had a times array. So if a doctor is available for any given slot, we make that entry in the array. Let's see what problem we can have with this approach. Assuming we can have around 1,000 doctors in our system. And each doctor can have seven JSON objects to represent time for a week. So one JSON objects to represent time for a day and so on for the whole week, which summed up to a total of 7,000 JSON objects and ultimately consumed too much memory and speed for storing and processing them and not suitable for mobile devices. Let's see how we can fix this. So remember in the talk, I told you the most important benefit, getting innovative ideas to solve your problem. So what I thought was, what if we can represent time as binary data instead of actual values? So if a doctor was available for any given R slot, we stored that as 1. If the doctor is not available for the next lot, we stored that as 0. And we keep doing that till we exhaust all the 24 digits and which gave us finally a 24 digit binary value for the entire day. Each doctor will have now seven binary values instead of seven JSON objects to represent time for a week. So on the server, we have a binary digit for, let's say, doctor A for September 18, which is today. We have a different binary values for another doctor for the same day. And what we did was, we performed a logical OR operation, which gave us the combined availability of all the doctors in our system. And we passed that value to integer because binary value was of a 24 digit, so we can still decrease the size, which gave us an integer value such as this. And we now send this value to the client. On the client, we passed it back to Boolean, which gave us the binary value. And we prefixed that with 0 if the length was less than 24. Our week data now looks like something like this. We have comma-separated integer values. So we have seven integer values to represent times for a day. The maximum value we can ever get using this approach if the doctor is available for all the r slots of a day was this, which is big enough but still smaller than having 7,000 JSON objects. So if we compare with the previous approach that we had, we had 7,000 JSON objects. And now we have seven integer value, comma-separated integer value, which is quite efficient, both in terms of memory and speed. So to conclude this talk, I would like to say, JavaScript has limitations, and so does every other language has. So whenever you get stuck around a problem, which you feel is because of the language, don't just give up. Because most of the time, the problem is with our implementation rather than the language itself. Use JavaScript flexibility to invent new data types. So as we have seen in the Tory example, we invented a new type, which we can call as numeric string. So JavaScript may lags behind other languages in terms of processing power, but it gets a huge advantage when it comes to its flexibility, and we can use that. It makes sense to use JavaScript for competitive programming. So no matter what the statistics say, or no matter what the tradition has been so far, if you use JavaScript for competitive programming, you may not become a very efficient competitive programmer, but you can still use that experience as a benefit for your building of applications. Thank you. Questions? Hi, Brine. So I'm not sure about the algorithm you described for calculating bigger factorials, because that's a problem with C and C++ also. It's more related to data overflow, data type overflow, because in C++ also, if you try to calculate bigger factorials, it overflows out of the long, long int or int data type. So that's a more generalized problem, and also how does JavaScript makes it better for C++, better than C++, or C++ better than for competitive programming? Because I'm not sure it's particular to algorithm, and rather than the language implementation. So what was the point you were trying to make there? So in languages like Java, they have types like begin, which lets you store even bigger numeric values. While in JavaScript, you only have single number type. So the advantage of using JavaScript, I'm not promoting JavaScript for competitive programming, but the advantage you get here is I was looking for a workaround problem, workaround for this problem, and what they were doing was on C++, they were just calculating somehow calculating the last 10 digits, dumping it to the console, then the remaining 10 digits, and dumping to the console, so on, they were trying to calculate the factorial. But in JavaScript, what you can do is because of the JavaScript flexibility, you can use string as numeric values, which you can't do in C and C++. And you can hold on as long as a big value, and also with a very small amount of memory usage. Thank you. Thank you, Pranay. The JS4 networking party happens tonight at 7.