SlideShare a Scribd company logo
1 of 19
Download to read offline
Counting Valleys
Programming problem in hackerrank.com
Description
Imagine a straight line, a sea level.
If we go Up, we’re above the sea
level, in a hill.
While there, if we go down, we got to
sea level again.
If we go down again, we’re below sea
level, in a valley.
If we go up again, we’re again at sea
level.
Start
Up
Down
Down
Up
Description
Problem is the following: Given a series of movements, represented by a string, in
which “U” means “go Up” and “D” means “go Down”, count all the times we’re
below sea level, in a valley.
For example: DUDDUUUD
Meaning: Down, Up, Down, Down, Up, Up, Up, Down
1
2
The number of valleys is 2
Solution - Data Structure
First thing is to determined which data structure we need.
As we view in the graphical representation, we can count the valleys in a linear
way, by an iteration of every command (Up or Down).
So the data structure we need is an array. A string is an array of characters.
D U D D U U U D
Solution - Algorithm
Next to define is the steps we’re going to perform.
- To solve the problem, we need to know when we’re down the sea level, and
when we reach sea level again.
- Easiest way is assign a value to up’s and downs, 1 and -1, and sum them on
every iteration.
- If the sum is smaller than 0 (negative), we’re in a valley.
- We should count the valley once we reach sea level again, when sum is equal
to zero once more time.
Now, let’s put our algorithm to test.
Solution - Desktop Test
Char Value Sum In a Valley Valley count
D -1 -1 Yes 0
U 1 0 No 1
D -1 -1 Yes 1
D -1 -2 Yes 1
U 1 -1 Yes 1
U 1 0 No 2
U 1 1 No 2
D -1 0 No 2
Solution - Codification
Once our algorithm is validated, then
we can code it with confidence.
The language used here is Javascript.
Good practices is write the algorithm in
comments, to describe what the
program is doing.
We can easily follow the commented
code to review the algorithm.
function countingValleys
(steps, path) {
// to store the amount of valleys
let valleys = 0;
// to sum the value of every step
let sum = 0;
// to know if we're on a valley
let inValley = 0;
// loop the steps
for (let step of path) {
// add the step value
if (step == 'U') {
// if up, positive
sum++;
} else {
// if down, negative
sum--;
} // end if step is 'U'
// if we were on a valley and reach sea level
if (inValley && sum == 0) {
// value counter increase
valleys
++;
// we're not in a valley anymore
inValley = 0;
} // end if we were in a valley
// if the sum is negative
if (sum < 0) {
// we're below sea level, in a valley
inValley = 1;
} // end if sum < 0
} // end for each step
// we return a number of valleys
return valleys
;
} // end function counting valleys
Solution - Codification
First thing we got to do is to get
the signature right, meaning the
parameters and the output are
setup correctly.
/**
* We receive a number of steps
* and a string representing a path
*/
function countingValleys(steps, path) {
// we return a number of valleys
return valleys;
} // end function counting valleys
Solution - Codification
As we’re starting with algorithms
and data structures, a good
practice is to declare all variable
we’re gonna need and give them
descriptive names.
Also, it will be easier for the the
person reading the code to follow
the algorithm.
...
// to store the amount of valleys
let valleys = 0;
// to sum the value of every step
let sum = 0;
// to know if we're on a valley
let inValley = 0;
...
Solution - Codification
Now we implement the validated
algorithm.
Code can be pretty short and
concise, nevertheless the example
here is verbose for clarification
purposes.
Readability and clarity are key
whenever you’re learning or
reviewing a piece of code.
You can always refactor it to make it
neat and simple.
...
// loop the steps
for (let step of path) {
// add the step value
if (step == 'U') {
// if up, positive
sum++;
} else {
// if down, negative
sum--;
} // end if step is 'U'
// if we were on a valley and reach sea level
if (inValley && sum == 0) {
// value counter increase
valleys++;
// we're not in a valley anymore
inValley = 0;
} // end if we were in a valley
// if the sum is negative
if (sum < 0) {
// we're below sea level, in a valley
inValley = 1;
} // end if sum < 0
} // end for each step
...
Solution - Codification
Let’s review the cycle process.
For each step (letter) of the path
(the string), we must perform the
described algorithm,
...
// loop the steps
for (let step of path) {
// here we code
// the algorithm
} // end for each step
...
Solution - Codification
Inside the loop, first thing we’re
gonna need is determine the
movement.
If ‘Up’, add one unit to the total
sum, otherwise (is ‘Down’),
subtract one unit.
...
// add the step value
if (step == 'U') {
// if up, positive
sum++;
} else {
// if down, negative
sum--;
} // end if step is 'U'
...
Solution - Codification
Next step is search if we must add
any valleys.
To do this, apply the condition:
- If we were in a valley, and
now we’re again at sea level,
add a valley to the counter.
- As we reach sea level, now
we’re not in a valley.
...
// if we were on a valley
// and reach sea level
if (inValley && sum == 0) {
// value counter increase
valleys++;
// we're not
// in a valley anymore
inValley = 0;
} // end if we were in a valley
...
Solution - Codification
Finally, if the total sum of values
for the positions (up’s and down’s)
is negative, we’re in a valley.
And with this we’ve finished
implemented the algorithm for the
iteration.
After cycle ends, program return
the counter of valleys.
Let’s view a diagram of our
solution.
...
// if the sum is negative
if (sum < 0) {
// we're below
// sea level
// in a valley
inValley = 1;
} // end if sum < 0
...
Flow Diagram - Code
function countingValleys
(steps, path) {
// to store the amount of valleys
let valleys = 0;
// to sum the value of every step
let sum = 0;
// to know if we're on a valley
let inValley = 0;
// loop the steps
for (let step of path) {
// add the step value
if (step == 'U') {
// if up, positive
sum++;
} else {
// if down, negative
sum--;
} // end if step is 'U'
// if we were on a valley and reach sea level
if (inValley && sum == 0) {
// value counter increase
valleys
++;
// we're not in a valley anymore
inValley = 0;
} // end if we were in a valley
// if the sum is negative
if (sum < 0) {
// we're below sea level, in a valley
inValley = 1;
} // end if sum < 0
} // end for each step
// we return a number of valleys
return valleys
;
} // end function counting valleys
Time and Space complexity
As we only need to loop the array once, the time complexity is O(n).
As we do not need to copy the array, the space complexity is O(n) also.
This is an efficient algorithm.
D U D D U U U D
Test the solution
Our algorithm most run for a variety of cases:
- Plain straight, happy path cases.
- Edge cases (very short of very large).
- Empty of null cases (what if no input is provided).
- Time execution and memory allocation.
- Must perform in a short amount of time and with as little resources as possible.
We need to know the answer beforehand in order to be able to test it.
There’s a myriad of testing tools, or we can simple code a test function that
evaluates the code and check against a pre-defined answer.
Key Takeaways
- Understand and create a clear image of the problem in your mind.
- Draw it helps a lot.
- Determine the data structure to use is solve the half of the problem.
- Write the algorithm steps and perform a desktop test to validate it before
writing the code.
- Write clear and readable code, with comments about what the program is
doing.
- Perform tests for major categories of problems, short, long, straight-forward,
special cases.
Thank you.
@espino316
allmylinks.com/luisespino

More Related Content

Similar to Counting valleys from Hackerrank solution explained (9)

Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 
DSL in scala
DSL in scalaDSL in scala
DSL in scala
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
Anu DAA i1t unit
Anu DAA i1t unitAnu DAA i1t unit
Anu DAA i1t unit
 
3. Training Artificial Neural Networks.pptx
3. Training Artificial Neural Networks.pptx3. Training Artificial Neural Networks.pptx
3. Training Artificial Neural Networks.pptx
 
Bellmon Ford Algorithm
Bellmon Ford AlgorithmBellmon Ford Algorithm
Bellmon Ford Algorithm
 
Scilab as a calculator
Scilab as a calculatorScilab as a calculator
Scilab as a calculator
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 

More from Luis Martín Espino Rivera

More from Luis Martín Espino Rivera (7)

Los principios y la alegria de programar
Los principios y la alegria de programarLos principios y la alegria de programar
Los principios y la alegria de programar
 
Lo que debes saber antes de iniciar un negocio por internet... Técnologicamen...
Lo que debes saber antes de iniciar un negocio por internet... Técnologicamen...Lo que debes saber antes de iniciar un negocio por internet... Técnologicamen...
Lo que debes saber antes de iniciar un negocio por internet... Técnologicamen...
 
High Performance Organization Culture Proposal
High Performance Organization Culture ProposalHigh Performance Organization Culture Proposal
High Performance Organization Culture Proposal
 
Quick answers to Angular2+ Interview Questions
Quick answers to Angular2+ Interview QuestionsQuick answers to Angular2+ Interview Questions
Quick answers to Angular2+ Interview Questions
 
MeritocraciaYA Cultura Organizacional Ciudadana
MeritocraciaYA Cultura Organizacional CiudadanaMeritocraciaYA Cultura Organizacional Ciudadana
MeritocraciaYA Cultura Organizacional Ciudadana
 
10 cosas que debes saber para contratar a un diseñador web
10 cosas que debes saber para contratar a un diseñador web10 cosas que debes saber para contratar a un diseñador web
10 cosas que debes saber para contratar a un diseñador web
 
Exportar información de Sql Server a Excel
Exportar información de Sql Server a ExcelExportar información de Sql Server a Excel
Exportar información de Sql Server a Excel
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Counting valleys from Hackerrank solution explained

  • 2. Description Imagine a straight line, a sea level. If we go Up, we’re above the sea level, in a hill. While there, if we go down, we got to sea level again. If we go down again, we’re below sea level, in a valley. If we go up again, we’re again at sea level. Start Up Down Down Up
  • 3. Description Problem is the following: Given a series of movements, represented by a string, in which “U” means “go Up” and “D” means “go Down”, count all the times we’re below sea level, in a valley. For example: DUDDUUUD Meaning: Down, Up, Down, Down, Up, Up, Up, Down 1 2 The number of valleys is 2
  • 4. Solution - Data Structure First thing is to determined which data structure we need. As we view in the graphical representation, we can count the valleys in a linear way, by an iteration of every command (Up or Down). So the data structure we need is an array. A string is an array of characters. D U D D U U U D
  • 5. Solution - Algorithm Next to define is the steps we’re going to perform. - To solve the problem, we need to know when we’re down the sea level, and when we reach sea level again. - Easiest way is assign a value to up’s and downs, 1 and -1, and sum them on every iteration. - If the sum is smaller than 0 (negative), we’re in a valley. - We should count the valley once we reach sea level again, when sum is equal to zero once more time. Now, let’s put our algorithm to test.
  • 6. Solution - Desktop Test Char Value Sum In a Valley Valley count D -1 -1 Yes 0 U 1 0 No 1 D -1 -1 Yes 1 D -1 -2 Yes 1 U 1 -1 Yes 1 U 1 0 No 2 U 1 1 No 2 D -1 0 No 2
  • 7. Solution - Codification Once our algorithm is validated, then we can code it with confidence. The language used here is Javascript. Good practices is write the algorithm in comments, to describe what the program is doing. We can easily follow the commented code to review the algorithm. function countingValleys (steps, path) { // to store the amount of valleys let valleys = 0; // to sum the value of every step let sum = 0; // to know if we're on a valley let inValley = 0; // loop the steps for (let step of path) { // add the step value if (step == 'U') { // if up, positive sum++; } else { // if down, negative sum--; } // end if step is 'U' // if we were on a valley and reach sea level if (inValley && sum == 0) { // value counter increase valleys ++; // we're not in a valley anymore inValley = 0; } // end if we were in a valley // if the sum is negative if (sum < 0) { // we're below sea level, in a valley inValley = 1; } // end if sum < 0 } // end for each step // we return a number of valleys return valleys ; } // end function counting valleys
  • 8. Solution - Codification First thing we got to do is to get the signature right, meaning the parameters and the output are setup correctly. /** * We receive a number of steps * and a string representing a path */ function countingValleys(steps, path) { // we return a number of valleys return valleys; } // end function counting valleys
  • 9. Solution - Codification As we’re starting with algorithms and data structures, a good practice is to declare all variable we’re gonna need and give them descriptive names. Also, it will be easier for the the person reading the code to follow the algorithm. ... // to store the amount of valleys let valleys = 0; // to sum the value of every step let sum = 0; // to know if we're on a valley let inValley = 0; ...
  • 10. Solution - Codification Now we implement the validated algorithm. Code can be pretty short and concise, nevertheless the example here is verbose for clarification purposes. Readability and clarity are key whenever you’re learning or reviewing a piece of code. You can always refactor it to make it neat and simple. ... // loop the steps for (let step of path) { // add the step value if (step == 'U') { // if up, positive sum++; } else { // if down, negative sum--; } // end if step is 'U' // if we were on a valley and reach sea level if (inValley && sum == 0) { // value counter increase valleys++; // we're not in a valley anymore inValley = 0; } // end if we were in a valley // if the sum is negative if (sum < 0) { // we're below sea level, in a valley inValley = 1; } // end if sum < 0 } // end for each step ...
  • 11. Solution - Codification Let’s review the cycle process. For each step (letter) of the path (the string), we must perform the described algorithm, ... // loop the steps for (let step of path) { // here we code // the algorithm } // end for each step ...
  • 12. Solution - Codification Inside the loop, first thing we’re gonna need is determine the movement. If ‘Up’, add one unit to the total sum, otherwise (is ‘Down’), subtract one unit. ... // add the step value if (step == 'U') { // if up, positive sum++; } else { // if down, negative sum--; } // end if step is 'U' ...
  • 13. Solution - Codification Next step is search if we must add any valleys. To do this, apply the condition: - If we were in a valley, and now we’re again at sea level, add a valley to the counter. - As we reach sea level, now we’re not in a valley. ... // if we were on a valley // and reach sea level if (inValley && sum == 0) { // value counter increase valleys++; // we're not // in a valley anymore inValley = 0; } // end if we were in a valley ...
  • 14. Solution - Codification Finally, if the total sum of values for the positions (up’s and down’s) is negative, we’re in a valley. And with this we’ve finished implemented the algorithm for the iteration. After cycle ends, program return the counter of valleys. Let’s view a diagram of our solution. ... // if the sum is negative if (sum < 0) { // we're below // sea level // in a valley inValley = 1; } // end if sum < 0 ...
  • 15. Flow Diagram - Code function countingValleys (steps, path) { // to store the amount of valleys let valleys = 0; // to sum the value of every step let sum = 0; // to know if we're on a valley let inValley = 0; // loop the steps for (let step of path) { // add the step value if (step == 'U') { // if up, positive sum++; } else { // if down, negative sum--; } // end if step is 'U' // if we were on a valley and reach sea level if (inValley && sum == 0) { // value counter increase valleys ++; // we're not in a valley anymore inValley = 0; } // end if we were in a valley // if the sum is negative if (sum < 0) { // we're below sea level, in a valley inValley = 1; } // end if sum < 0 } // end for each step // we return a number of valleys return valleys ; } // end function counting valleys
  • 16. Time and Space complexity As we only need to loop the array once, the time complexity is O(n). As we do not need to copy the array, the space complexity is O(n) also. This is an efficient algorithm. D U D D U U U D
  • 17. Test the solution Our algorithm most run for a variety of cases: - Plain straight, happy path cases. - Edge cases (very short of very large). - Empty of null cases (what if no input is provided). - Time execution and memory allocation. - Must perform in a short amount of time and with as little resources as possible. We need to know the answer beforehand in order to be able to test it. There’s a myriad of testing tools, or we can simple code a test function that evaluates the code and check against a pre-defined answer.
  • 18. Key Takeaways - Understand and create a clear image of the problem in your mind. - Draw it helps a lot. - Determine the data structure to use is solve the half of the problem. - Write the algorithm steps and perform a desktop test to validate it before writing the code. - Write clear and readable code, with comments about what the program is doing. - Perform tests for major categories of problems, short, long, straight-forward, special cases.