Git Product home page Git Product logo

algorithms's Introduction

Algorithmic Toolbox

Course: https://www.coursera.org/learn/algorithmic-toolbox

Specialisation: https://www.coursera.org/specializations/data-structures-algorithms

Week 1 Problems

Stress Testing Technique

  • Helps debug one solution by comparing it with another
  • Keep two or more solutions (one you intend to use/submit, one brute force approach)
  • Generate random inputs conforming to the criteria of the problem
  • Run an infinite loop which generates the random input and runs the two functions
  • Break whenever there is a mismatch in the outputs of the two approaches
  • This means either one or both approaches were wrong
  • Manually verify solution for that input and fix the appropriate function(s)
  • If the while loop does not break, wait for a few seconds (say ~10s)
  • After that force exit the execution (Ctrl-C)
  • We can now be sure that our solution is correct
  • Note:
    • Make sure to test for the extremes of the input ranges (Esp. higher numbers)
    • Stress testing might take time to report a difference
    • This is because of corner cases (Ex: Occurrence of two same numbers)
    • When you are unable to find errors in stress test: Try to run the tests on a more focused input range
    • Example: Working with graphs? Generate disconnected graphs, bipartite graphs, cyclic graphs, etc

Problem Solving Approach to Submit Solutions

  • Test on the examples from the problem statement.
  • Then make a few other small tests, solve them manually and check that your program outputs the correct answer.
  • Generate a big input and launch your program to check that it works fast enough and doesn't consume too much memory.
  • Test for corner cases: smallest allowed values and largest allowed values of all input parameters, equal numbers in the input, very long strings, etc.
  • Then make a stress test.
  • After all these tests passed, submit the solution.

1. Maximum pairwise product

Given a sequence of numbers, determine the maximum pair product from it.

Naive solution

Multiply every number with every other number. During each iteration, replace the result with the current product of two numbers if it is more than it. We will have two for loops to multiply every number with every other number. Therefore, TC = O(n^2)

Optimal Solution

function MaxPairwiseProduct(numbers) {
  var max = -1
  var secondMax = -1
  numbers.forEach(n => {
    if (n >= max) {
      secondMax = max
      max = n
    } else if (n > secondMax) {
      secondMax = n
    }
  })
  return max * secondMax
}

algorithms's People

Contributors

pushkar100 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.