Skip to content

Instantly share code, notes, and snippets.

@deb1990
Last active February 20, 2021 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deb1990/8697db2d62811048e5efde81d5db8d1d to your computer and use it in GitHub Desktop.
Save deb1990/8697db2d62811048e5efde81d5db8d1d to your computer and use it in GitHub Desktop.
SlashData

1. Prime Number

The 10001st Prime Number is 104743. Please find the code in primenumber.js file.

Technical Notes
  • In the code a cache is used to do the calculation more efficiently. When calculating the number for the 1st time, it takes around 23.35302734375ms, but if we run it again, it takes just 0.260986328125ms.
  • If next time we try to get the 10002nd prime number, The program will only calculate the 10002nd prime, because the previous 10001 numbers are already saved in the cache. This is again a huge performance benefit.

2. When would you choose a NoSQL database instead of a relational database and why?

  • In NoSQL each document can have its own unique structure. So when the schema of the data is not consistent and can change at any time, NoSQL is preferred. Its very flexible to add new kind of data without affecting existing data.
  • When budget is less but a lot of data needs to be managed. Genrally NoSQL is much cheaper specially because of its Horizontal Scaling feature.
  • When the team mainly consist of frontend devs. Its very easy to learn NoSQL as it uses the JS Object/JSON structure.

3. If you have problems with the performance of SQL queries on data that changes very infrequently, what options are there for improving the performance?

  • By turning on DB caching

4. What are the most important things to consider when designing a web application that has to work well on mobile devices?

  • App is Responsive for all Major device sizes
  • Mobile has limited bandwidth and peformance in many cases, so
    • Create progressive web apps using service workers to cache the results
    • Minify/bundle JS and CSS Files
    • Reduce network requests
    • Serve optimized images for mobile
    • Optimize the cpu performance by writing efficient code(Use profilers to do this)
  • Use Native elements for better experience
    • Native Datepickers
    • Send Notifications
    • Save Icon to Desktop
  • Have a separate domain for mobile version

5. What would you consider an effective development process and collaboration tools for a small remote team of 3 or 4 developers?

  • I currently use JIRA, Github, Hipchat for the remote work in my organization. While it is very good, but it would be an overkill for 3-4 devs. So I would consider trello + github + free version of slack.
  • For development process Agile is already proven. Creating user stories, estimation, standup calls, and iterations are very good for any kind of team.
// Cache the already calculated prime numbers
var cachedPrimeNumbers = cachedPrimeNumbers || [];
/**
* Checks if the given number is prime
*
* @param {Int} n - the number which is to be checked
* @return {Boolean}
*/
function checkPrime(n) {
for (var i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
return false;
}
}
return true;
}
/**
* Returns the nth prime number
*
* @param {Int} n
* @return {Int}
*/
function getNthPrime(n) {
// Check if the number is present in the cache
if (cachedPrimeNumbers[n - 1]) {
return (cachedPrimeNumbers[n - 1]);
}
/**
* If Previously m number of prime number are calculated,
* only count n-m
*/
var primeNumberCount = cachedPrimeNumbers.length;
var nextNumberToTest = cachedPrimeNumbers.length > 0 ? cachedPrimeNumbers[cachedPrimeNumbers.length - 1] : 1;
while (primeNumberCount < n) {
nextNumberToTest++;
if (checkPrime(nextNumberToTest)) {
cachedPrimeNumbers[primeNumberCount] = nextNumberToTest;
primeNumberCount++
}
}
return nextNumberToTest;
}
console.time('Time taken');
console.log(getNthPrime(10001));
console.timeEnd('Time taken');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment