SlideShare a Scribd company logo
How to Solve Problems
Problem Solving
Software University
https://softuni.bg
SoftUni Team
Technical Trainers
sli.do
#fund-common
Questions?
2
Table of Content
1. What Is a Problem?
2. Stages of Problem Solving
3. Complexity of a Problem
4. Example Problems
5. From Chaotic to Methodological Approach
3
What Is a Problem?
Current and Desired State Diff
 Definition - a doubtful or difficult matter,
requiring a solution
 Goals - anything you wish to achieve,
anywhere you want to be
 Barriers - obstacles that prevent the
achievement of goals
What Is a Problem?
5
Without goals and barriers
there is no problem
Stages
Stages of Solving a Problem
 Define the problem
 Analyse the problem
 Identify potential solutions
 Evaluate and choose the best solution
 Plan action
 Implement and review results
Solving a Problem
 What are the steps to cross the street successfully ?
Problem Street Crossing
8
 Recognizing that there is a problem, identifying the nature
of the problem, defining the problem
Problem Identification
9
Ascertain
the
objective
of the
decision
maker
Understand
the
background
of the
problem
Isolate
and
identify
the
problem
Determine
the unit of
analysis
and the
relevant
variables
State and
research
the
objective
 Observation
 Inspection
 Fact-finding
 Developing a clear picture
of the problem
Structure of the Problem
10
 Generating a range of possible courses of action and trying to
evaluate them
 Analyzing the different possible solutions and choosing the
best one
Solutions and Decisions
11
 Connect all the nine dots,
using only 4 lines
Nine Dots Problem
12
 Let's think outside the box!
 What has changed?
 Our point of view
Nine Dots Problem
13
Examples
Solving Problems
The Missing Piece
15
 You are mixing cement and the recipe calls for five gallons of water
 You have a garden hose giving you all the water you need
 The problem is that you only have a four-gallon bucket and
a seven-gallon bucket and neither has graduation marks
 Find a method to measure five gallons
Five Gallons
16
4 gal 7 gal
 You have 10 stacks of 10 gold coins
 All the coins in one of these stacks are counterfeit,
all the other coins are not
 A real coin weighs 10 grams
 A counterfeit coin weighs 11 grams
 You can use an extremely
accurate digital weighing machine only once
 How do you determine which set of
10 coins is faulty?
10 Piles of 10 Coins One of Which Is Fake
17
Solutions
The Missing Piece
19
The Missing Piece
20
 The gradient of the teal hypotenuse is different than the
gradient of the red hypotenuse
 Fill the 4-gallon bucket
 Empty the 4-gallon bucket into the 7-gallon bucket
 Fill the 4-gallon bucket
 Pour the 3 gallons from the 4-gallon bucket
into the 7-gallon bucket
 Empty the 7-gallon bucket
 Pour the 1 gallon from the 4-gallon bucket into the 7-gallon bucket
 Fill the 4-gallon bucket
 Empty the 4-gallon bucket into the 7-gallon bucket
 We have solved the problem
Five Gallons
21
0 gal
0 gal
4 gal 4 gal
1 gal
7 gal1 gal
5 gal
 We take one coin from pile #1, two coins from pile #2, three coins
from pile #3 and so on until we have 10 coins from pile #10
 This gives us 55 coins which if they were all pure would give us 550g
10 Piles of 10 Coins One of Which Is Fake (1)
22
10 Piles of 10 Coins One of Which Is Fake (2)
23
 However, some of them will be fake
 Let's say we place them all on the scale, for our one and only
weighing, and it reads 553g
 The only possible way this could have happened is if we placed
3 counterfeit coins on the balance, and that means that
pile #3 is counterfeit
Solving
From Chaotic to Methodological Approach
Read and Analyze the Problems
25
 Consider you are at a computer programming exam or contest
 You have 5 problems to solve in 6 hours
 First read carefully all problems and try to estimate how
complex each of them is
 Read the requirements, don't invent them!
 Start solving the easiest / fastest to solve problem first!
 Leave the most complex / slow to solve problem last!
 Approach the next problem when the previous is well tested
Use a Sheet of Paper and a Pen
26
 Never start solving a problem without a sheet of paper + a pen
 You need to sketch your ideas
 Paper and pen is the best visualization tool
 Allows your brain to think efficiently
 Paper works faster than keyboard / screen
 Other visualization tools could also work well
Prefer Squared Paper
27
 Squared paper works best for
algorithmic problems
 Easy to draw a table
 Easy to draw a coordinate
system with objects at it
 Easy to calculate distances
 Easy to sketch a problem and
solution idea
Paper and Pen
28
 Consider a "Cards Shuffle" problem
 We can sketch it to start thinking
 Some ideas immediately come, e.g.
 Split the deck into two parts and swap them multiple times
 Swap 2 random cards a random number of times
 Swap each card with a random card
Invent Ideas
Think-up, Invent Ideas and Check Them
Think Up, Invent and Try Ideas
30
 First take an example of the problem
 Sketch it on the sheet of paper
 Next try to invent some idea that works for your example
 Check if your idea will work for other examples
 Try to find a case that breaks your idea
 Try challenging examples and unusual cases
 If you find your idea incorrect, try to fix it
 Or just invent a new idea
Think Up, Invent and Try Ideas
31
 Idea #1: random number of times split the deck into left and
right part and swap them
 How to represent the cards?
 How to choose a random split point?
 How to perform the exchange?
 Idea #2: swap each card with a random card
 How many times to repeat this?
 Is this fast enough?
Think Up, Invent and Try Ideas
32
 Idea #3: swap 2 random cards a random number of times
 How to choose two random cards?
 How many times to repeat this?
 Idea #4: choose a random card and insert it in front of the deck
 How to choose random card?
 How many times to repeat this?
 Idea #5: do you have another idea?
Divide and Conquer
Decompose Problems into Manageable Pieces
Decompose the Problem
34
 Work decomposition is natural in engineering
 It happens every day in the industry
 Projects are decomposed into subprojects
 Complex problems could be decomposed into several smaller
sub-problems
 Technique known as "Divide and Conquer"
 Small problems could easily be solved
 Smaller sub-problems could be further decomposed as well
Divide and Conquer – Example
35
 Let's try idea #1:
 Split the deck into left and right part and swap them
(many times)
 Divide and conquer
 Sub-problem #1 (single exchange) – split the deck into two
random parts and exchange them
 Sub-problem #2 – choosing a random split point
 Sub-problem #3 – combining single exchanges
 How many times to perform "single exchange"?
Sub-Problem #1 (Single Exchange)
36
 Split the deck into two parts at random split point
 And exchange these 2 parts
 We visualize this by paper and pen:
Sub-Problem #2 (Single Exchange)
37
 Choosing a random split point
 Need to understand the concept of pseudo-random numbers
and how to use it
 In Internet lots of examples are available, some of them incorrect
 The class System.Random can do the job
 Important detail is that the Random class should be
instantiated only once
 Not at each random number generation
Sub-Problem #3 (Single Exchange)
38
 Combining a sequence of single exchanges to solve
the initial problem
 How many times to perform single exchanges to reliably
randomize the deck?
 N times (where N is the number of the cards) seems enough
 We have an algorithm:
 N times split at random position and exchange the left and right
parts of the deck
Check-Up Your Ideas
Don't Go Ahead Before Checking Your Ideas
Check-Up Your Ideas
40
 Check-up your ideas with examples
 It is better to find a problem before the idea is implemented
 When the code is written, changing your ideas radically costs a
lot of time and effort
 Carefully select examples for check-up
 Examples should be simple enough to be checked
by hand in a minute
 Examples should be complex enough to cover the most general
case, not just an isolated case
Check-Up Your Ideas – Example
41
 Let's check the idea:
 After 3 random splits and swaps we obtain the start position
 Seems like a serious problem!
Invent New Idea If Needed
42
 What to do when you find your idea is not working in all cases?
 Try to fix your idea
 Sometimes a small change could fix the problem
 Invent new idea and carefully check it
 Iterate
 Usually your first idea is not the best
 Invent ideas, check them, try various cases, find problems,
fix them, invent better ideas, etc.
Invent New Ideas – Example
43
 Invent a few new ideas:
 New idea #1 – multiple times select 2 random cards and
exchange them
 New idea #2 – multiple times select a random card and
exchange it with the first card
 New idea #3 – multiple times select a random card and move it
to an external list
 Let's check the new idea #2
 Is it correct?
Check-Up the New Idea – Example
44
Implementation
Coding and Testing Step-By-Step
Start Coding: Checklist
46
 Never start coding before you find a correct idea that will meet
the requirements
 What shall you write before you have a correct idea?
 Checklist to follow before start of coding:
 Ensure you understand the requirements well
 Ensure you have come up a good idea
 Ensure your idea is correct
 Ensure you know what data structures to use
 Ensure the performance will be sufficient
Implement Your Algorithm Step-By-Step
47
 "Step-by-step" approach is always better than
"build all, then test"
 Implement a piece of your program and test it
 Then implement another piece of the program and test it
 Finally put together all pieces and test it
 Small increments (steps) reveal errors early
 "Big bang" integration takes more time
Testing the Code
Thoroughly Test Your Solution
Thoroughly Test Your Solution
49
 Always test thoroughly your solution
 Invest in testing!
 One 90-100% solved problem could be better than 2 or 3
partially solved
 Testing an existing problem takes less time than solving another
problem from scratch
How to Test?
50
 Testing could not certify absence of defects
 It just reduces the defects’ rates
 Well tested solutions are more likely to be correct
 Start testing with a good representative of the general case
 Not a small isolated case, but a typical one
 Large enough test case, but small enough to be easily checkable
Read the Problem Statement
51
 Read carefully the problem statement
 Does your solution print exactly what is expected?
 Does your output follow the requested format?
 Did you remove your debug printouts?
 Be sure to solve the requested problem, not the problem
you think is requested!
 Example: "Write a program to print the number of
permutations of n elements" means to print a single number,
not a set of permutations!
 …
 …
 …
Summary
52
 Problem solving needs methodology:
 Understanding and analyzing problems
 Using a sheet of paper and a pen for sketching
 Thinking up, inventing and trying ideas
 Decomposing problems into sub-problems
 Selecting appropriate data structures
 Thinking about the efficiency and performance
 Implementing step-by-step
 Testing the nominal case, border cases and efficiency
 https://softuni.bg/courses/programming-fundamentals
SoftUni Diamond Partners
SoftUni Organizational Partners
 Software University – High-Quality Education and
Employment Opportunities
 softuni.bg
 Software University Foundation
 http://softuni.foundation/
 Software University @ Facebook
 facebook.com/SoftwareUniversity
 Software University Forums
 forum.softuni.bg
Trainings @ Software University (SoftUni)
 This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-NonCom
mercial-ShareAlike 4.0 International" license
License
57

More Related Content

Similar to Java Problem solving (20)

Csc 102 lecture note(introduction to problem solving)
Csc 102 lecture note(introduction to problem solving)Csc 102 lecture note(introduction to problem solving)
Csc 102 lecture note(introduction to problem solving)
Christopher Chizoba
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
Prof Ansari
 
CC-112-Lec.1.ppsx
CC-112-Lec.1.ppsxCC-112-Lec.1.ppsx
CC-112-Lec.1.ppsx
Aamir Shahzad
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
hamza javed
 
Quicksorter V2
Quicksorter V2Quicksorter V2
Quicksorter V2
Tony Tran
 
Programming in C - Problem Solving using C
Programming in C - Problem Solving using CProgramming in C - Problem Solving using C
Programming in C - Problem Solving using C
PoovizhiP1
 
Algorithms and how to write an algorithms
Algorithms and how to write an algorithmsAlgorithms and how to write an algorithms
Algorithms and how to write an algorithms
Ahmed Nobi
 
PPS_Unit 1.pptx
PPS_Unit 1.pptxPPS_Unit 1.pptx
PPS_Unit 1.pptx
KundanBhatkar
 
FDS Unit I_PPT.pptx
FDS Unit I_PPT.pptxFDS Unit I_PPT.pptx
FDS Unit I_PPT.pptx
sayalishivarkar1
 
Problem solving methodology
Problem solving methodologyProblem solving methodology
Problem solving methodology
Prof. Dr. K. Adisesha
 
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdfCSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
MisterPhilips
 
Visual Problem Solving (Lecture)
Visual Problem Solving (Lecture)Visual Problem Solving (Lecture)
Visual Problem Solving (Lecture)
Lawrence Wachs
 
Study Material for Problem Solving Techniques
Study Material for Problem Solving TechniquesStudy Material for Problem Solving Techniques
Study Material for Problem Solving Techniques
Bobby Murugesan
 
E book
E bookE book
E book
Lebohang Molapo
 
computational_thinking_gcse.pptx
computational_thinking_gcse.pptxcomputational_thinking_gcse.pptx
computational_thinking_gcse.pptx
birulangit23
 
Introductions to Problem solvings.pptx
Introductions  to  Problem solvings.pptxIntroductions  to  Problem solvings.pptx
Introductions to Problem solvings.pptx
Guna Dhondwad
 
Algorithms
AlgorithmsAlgorithms
Algorithms
suzzanj1990
 
Grokking Techtalk #45: First Principles Thinking
Grokking Techtalk #45: First Principles ThinkingGrokking Techtalk #45: First Principles Thinking
Grokking Techtalk #45: First Principles Thinking
Grokking VN
 
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptx
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptxChallenges-and-Consideration-in-Programming-Logic-and-Design...pptx
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptx
RusherGamer1
 
Algorithm itabq
Algorithm itabqAlgorithm itabq
Algorithm itabq
mckennadglyn
 
Csc 102 lecture note(introduction to problem solving)
Csc 102 lecture note(introduction to problem solving)Csc 102 lecture note(introduction to problem solving)
Csc 102 lecture note(introduction to problem solving)
Christopher Chizoba
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
Prof Ansari
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
hamza javed
 
Quicksorter V2
Quicksorter V2Quicksorter V2
Quicksorter V2
Tony Tran
 
Programming in C - Problem Solving using C
Programming in C - Problem Solving using CProgramming in C - Problem Solving using C
Programming in C - Problem Solving using C
PoovizhiP1
 
Algorithms and how to write an algorithms
Algorithms and how to write an algorithmsAlgorithms and how to write an algorithms
Algorithms and how to write an algorithms
Ahmed Nobi
 
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdfCSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
MisterPhilips
 
Visual Problem Solving (Lecture)
Visual Problem Solving (Lecture)Visual Problem Solving (Lecture)
Visual Problem Solving (Lecture)
Lawrence Wachs
 
Study Material for Problem Solving Techniques
Study Material for Problem Solving TechniquesStudy Material for Problem Solving Techniques
Study Material for Problem Solving Techniques
Bobby Murugesan
 
computational_thinking_gcse.pptx
computational_thinking_gcse.pptxcomputational_thinking_gcse.pptx
computational_thinking_gcse.pptx
birulangit23
 
Introductions to Problem solvings.pptx
Introductions  to  Problem solvings.pptxIntroductions  to  Problem solvings.pptx
Introductions to Problem solvings.pptx
Guna Dhondwad
 
Grokking Techtalk #45: First Principles Thinking
Grokking Techtalk #45: First Principles ThinkingGrokking Techtalk #45: First Principles Thinking
Grokking Techtalk #45: First Principles Thinking
Grokking VN
 
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptx
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptxChallenges-and-Consideration-in-Programming-Logic-and-Design...pptx
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptx
RusherGamer1
 

More from Intro C# Book (20)

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
Intro C# Book
 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming Code
Intro C# Book
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
Intro C# Book
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
Intro C# Book
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
Intro C# Book
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
Intro C# Book
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
Intro C# Book
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
Intro C# Book
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
Intro C# Book
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
Intro C# Book
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
Intro C# Book
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
Intro C# Book
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
Intro C# Book
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
Intro C# Book
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
Intro C# Book
 
01. Introduction to programming with java
01. Introduction to programming with java01. Introduction to programming with java
01. Introduction to programming with java
Intro C# Book
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
Intro C# Book
 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming Code
Intro C# Book
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
Intro C# Book
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
Intro C# Book
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
Intro C# Book
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
Intro C# Book
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
Intro C# Book
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
Intro C# Book
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
Intro C# Book
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
Intro C# Book
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
Intro C# Book
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
Intro C# Book
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
Intro C# Book
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
Intro C# Book
 
01. Introduction to programming with java
01. Introduction to programming with java01. Introduction to programming with java
01. Introduction to programming with java
Intro C# Book
 
Ad

Recently uploaded (17)

Internet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptxInternet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptx
cshumerabashir
 
3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx
islamicknowledge5224
 
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptxInter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
secretarysocom
 
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdfPredicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Behzad Hussain
 
10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx
EphraimOOghodero
 
MOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary detailsMOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary details
benamorraj
 
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
Taqyea
 
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
treyka
 
Vigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptxVigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptx
secretarysocom
 
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your FeedbackICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
APNIC
 
How to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real TalkHow to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real Talk
Cam Sites Expert
 
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
CartCoders
 
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animationUV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
17218
 
Google_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptxGoogle_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptx
ektadangwal2005
 
Cloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptxCloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptx
islamicknowledge5224
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
Networking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptxNetworking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptx
elestirmen
 
Internet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptxInternet_of_Things_Presentation_by-Humera.pptx
Internet_of_Things_Presentation_by-Humera.pptx
cshumerabashir
 
3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx3D Graphics an introduction and details .pptx
3D Graphics an introduction and details .pptx
islamicknowledge5224
 
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptxInter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
secretarysocom
 
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdfPredicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Predicting Site Quality Google Patent US9767157B2 - Behzad Hussain.pdf
Behzad Hussain
 
10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx
EphraimOOghodero
 
MOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary detailsMOBILE PHONE DATA presentation with all necessary details
MOBILE PHONE DATA presentation with all necessary details
benamorraj
 
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
最新版西班牙加泰罗尼亚国际大学毕业证(UIC毕业证书)原版定制
Taqyea
 
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
Darley - BSides Nairobi (2025-06-07) Epochalypse 2038 - Time is Not on Our Si...
treyka
 
Vigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptxVigilanti-Cura-Protecting-the-Faith.pptx
Vigilanti-Cura-Protecting-the-Faith.pptx
secretarysocom
 
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your FeedbackICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
ICP -2 Review – What It Is, and How to Participate and Provide Your Feedback
APNIC
 
How to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real TalkHow to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real Talk
Cam Sites Expert
 
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
What to Expect When Hiring Shopify Development Services_ A Technical Walkthro...
CartCoders
 
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animationUV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
17218
 
Google_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptxGoogle_Cloud_Computing_Fundamentals.pptx
Google_Cloud_Computing_Fundamentals.pptx
ektadangwal2005
 
Cloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptxCloud Computing - iCloud by Hamza Anwaar .pptx
Cloud Computing - iCloud by Hamza Anwaar .pptx
islamicknowledge5224
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
Networking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptxNetworking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptx
elestirmen
 
Ad

Java Problem solving

  • 1. How to Solve Problems Problem Solving Software University https://softuni.bg SoftUni Team Technical Trainers
  • 3. Table of Content 1. What Is a Problem? 2. Stages of Problem Solving 3. Complexity of a Problem 4. Example Problems 5. From Chaotic to Methodological Approach 3
  • 4. What Is a Problem? Current and Desired State Diff
  • 5.  Definition - a doubtful or difficult matter, requiring a solution  Goals - anything you wish to achieve, anywhere you want to be  Barriers - obstacles that prevent the achievement of goals What Is a Problem? 5 Without goals and barriers there is no problem
  • 7.  Define the problem  Analyse the problem  Identify potential solutions  Evaluate and choose the best solution  Plan action  Implement and review results Solving a Problem
  • 8.  What are the steps to cross the street successfully ? Problem Street Crossing 8
  • 9.  Recognizing that there is a problem, identifying the nature of the problem, defining the problem Problem Identification 9 Ascertain the objective of the decision maker Understand the background of the problem Isolate and identify the problem Determine the unit of analysis and the relevant variables State and research the objective
  • 10.  Observation  Inspection  Fact-finding  Developing a clear picture of the problem Structure of the Problem 10
  • 11.  Generating a range of possible courses of action and trying to evaluate them  Analyzing the different possible solutions and choosing the best one Solutions and Decisions 11
  • 12.  Connect all the nine dots, using only 4 lines Nine Dots Problem 12
  • 13.  Let's think outside the box!  What has changed?  Our point of view Nine Dots Problem 13
  • 16.  You are mixing cement and the recipe calls for five gallons of water  You have a garden hose giving you all the water you need  The problem is that you only have a four-gallon bucket and a seven-gallon bucket and neither has graduation marks  Find a method to measure five gallons Five Gallons 16 4 gal 7 gal
  • 17.  You have 10 stacks of 10 gold coins  All the coins in one of these stacks are counterfeit, all the other coins are not  A real coin weighs 10 grams  A counterfeit coin weighs 11 grams  You can use an extremely accurate digital weighing machine only once  How do you determine which set of 10 coins is faulty? 10 Piles of 10 Coins One of Which Is Fake 17
  • 20. The Missing Piece 20  The gradient of the teal hypotenuse is different than the gradient of the red hypotenuse
  • 21.  Fill the 4-gallon bucket  Empty the 4-gallon bucket into the 7-gallon bucket  Fill the 4-gallon bucket  Pour the 3 gallons from the 4-gallon bucket into the 7-gallon bucket  Empty the 7-gallon bucket  Pour the 1 gallon from the 4-gallon bucket into the 7-gallon bucket  Fill the 4-gallon bucket  Empty the 4-gallon bucket into the 7-gallon bucket  We have solved the problem Five Gallons 21 0 gal 0 gal 4 gal 4 gal 1 gal 7 gal1 gal 5 gal
  • 22.  We take one coin from pile #1, two coins from pile #2, three coins from pile #3 and so on until we have 10 coins from pile #10  This gives us 55 coins which if they were all pure would give us 550g 10 Piles of 10 Coins One of Which Is Fake (1) 22
  • 23. 10 Piles of 10 Coins One of Which Is Fake (2) 23  However, some of them will be fake  Let's say we place them all on the scale, for our one and only weighing, and it reads 553g  The only possible way this could have happened is if we placed 3 counterfeit coins on the balance, and that means that pile #3 is counterfeit
  • 24. Solving From Chaotic to Methodological Approach
  • 25. Read and Analyze the Problems 25  Consider you are at a computer programming exam or contest  You have 5 problems to solve in 6 hours  First read carefully all problems and try to estimate how complex each of them is  Read the requirements, don't invent them!  Start solving the easiest / fastest to solve problem first!  Leave the most complex / slow to solve problem last!  Approach the next problem when the previous is well tested
  • 26. Use a Sheet of Paper and a Pen 26  Never start solving a problem without a sheet of paper + a pen  You need to sketch your ideas  Paper and pen is the best visualization tool  Allows your brain to think efficiently  Paper works faster than keyboard / screen  Other visualization tools could also work well
  • 27. Prefer Squared Paper 27  Squared paper works best for algorithmic problems  Easy to draw a table  Easy to draw a coordinate system with objects at it  Easy to calculate distances  Easy to sketch a problem and solution idea
  • 28. Paper and Pen 28  Consider a "Cards Shuffle" problem  We can sketch it to start thinking  Some ideas immediately come, e.g.  Split the deck into two parts and swap them multiple times  Swap 2 random cards a random number of times  Swap each card with a random card
  • 29. Invent Ideas Think-up, Invent Ideas and Check Them
  • 30. Think Up, Invent and Try Ideas 30  First take an example of the problem  Sketch it on the sheet of paper  Next try to invent some idea that works for your example  Check if your idea will work for other examples  Try to find a case that breaks your idea  Try challenging examples and unusual cases  If you find your idea incorrect, try to fix it  Or just invent a new idea
  • 31. Think Up, Invent and Try Ideas 31  Idea #1: random number of times split the deck into left and right part and swap them  How to represent the cards?  How to choose a random split point?  How to perform the exchange?  Idea #2: swap each card with a random card  How many times to repeat this?  Is this fast enough?
  • 32. Think Up, Invent and Try Ideas 32  Idea #3: swap 2 random cards a random number of times  How to choose two random cards?  How many times to repeat this?  Idea #4: choose a random card and insert it in front of the deck  How to choose random card?  How many times to repeat this?  Idea #5: do you have another idea?
  • 33. Divide and Conquer Decompose Problems into Manageable Pieces
  • 34. Decompose the Problem 34  Work decomposition is natural in engineering  It happens every day in the industry  Projects are decomposed into subprojects  Complex problems could be decomposed into several smaller sub-problems  Technique known as "Divide and Conquer"  Small problems could easily be solved  Smaller sub-problems could be further decomposed as well
  • 35. Divide and Conquer – Example 35  Let's try idea #1:  Split the deck into left and right part and swap them (many times)  Divide and conquer  Sub-problem #1 (single exchange) – split the deck into two random parts and exchange them  Sub-problem #2 – choosing a random split point  Sub-problem #3 – combining single exchanges  How many times to perform "single exchange"?
  • 36. Sub-Problem #1 (Single Exchange) 36  Split the deck into two parts at random split point  And exchange these 2 parts  We visualize this by paper and pen:
  • 37. Sub-Problem #2 (Single Exchange) 37  Choosing a random split point  Need to understand the concept of pseudo-random numbers and how to use it  In Internet lots of examples are available, some of them incorrect  The class System.Random can do the job  Important detail is that the Random class should be instantiated only once  Not at each random number generation
  • 38. Sub-Problem #3 (Single Exchange) 38  Combining a sequence of single exchanges to solve the initial problem  How many times to perform single exchanges to reliably randomize the deck?  N times (where N is the number of the cards) seems enough  We have an algorithm:  N times split at random position and exchange the left and right parts of the deck
  • 39. Check-Up Your Ideas Don't Go Ahead Before Checking Your Ideas
  • 40. Check-Up Your Ideas 40  Check-up your ideas with examples  It is better to find a problem before the idea is implemented  When the code is written, changing your ideas radically costs a lot of time and effort  Carefully select examples for check-up  Examples should be simple enough to be checked by hand in a minute  Examples should be complex enough to cover the most general case, not just an isolated case
  • 41. Check-Up Your Ideas – Example 41  Let's check the idea:  After 3 random splits and swaps we obtain the start position  Seems like a serious problem!
  • 42. Invent New Idea If Needed 42  What to do when you find your idea is not working in all cases?  Try to fix your idea  Sometimes a small change could fix the problem  Invent new idea and carefully check it  Iterate  Usually your first idea is not the best  Invent ideas, check them, try various cases, find problems, fix them, invent better ideas, etc.
  • 43. Invent New Ideas – Example 43  Invent a few new ideas:  New idea #1 – multiple times select 2 random cards and exchange them  New idea #2 – multiple times select a random card and exchange it with the first card  New idea #3 – multiple times select a random card and move it to an external list  Let's check the new idea #2  Is it correct?
  • 44. Check-Up the New Idea – Example 44
  • 46. Start Coding: Checklist 46  Never start coding before you find a correct idea that will meet the requirements  What shall you write before you have a correct idea?  Checklist to follow before start of coding:  Ensure you understand the requirements well  Ensure you have come up a good idea  Ensure your idea is correct  Ensure you know what data structures to use  Ensure the performance will be sufficient
  • 47. Implement Your Algorithm Step-By-Step 47  "Step-by-step" approach is always better than "build all, then test"  Implement a piece of your program and test it  Then implement another piece of the program and test it  Finally put together all pieces and test it  Small increments (steps) reveal errors early  "Big bang" integration takes more time
  • 48. Testing the Code Thoroughly Test Your Solution
  • 49. Thoroughly Test Your Solution 49  Always test thoroughly your solution  Invest in testing!  One 90-100% solved problem could be better than 2 or 3 partially solved  Testing an existing problem takes less time than solving another problem from scratch
  • 50. How to Test? 50  Testing could not certify absence of defects  It just reduces the defects’ rates  Well tested solutions are more likely to be correct  Start testing with a good representative of the general case  Not a small isolated case, but a typical one  Large enough test case, but small enough to be easily checkable
  • 51. Read the Problem Statement 51  Read carefully the problem statement  Does your solution print exactly what is expected?  Does your output follow the requested format?  Did you remove your debug printouts?  Be sure to solve the requested problem, not the problem you think is requested!  Example: "Write a program to print the number of permutations of n elements" means to print a single number, not a set of permutations!
  • 52.  …  …  … Summary 52  Problem solving needs methodology:  Understanding and analyzing problems  Using a sheet of paper and a pen for sketching  Thinking up, inventing and trying ideas  Decomposing problems into sub-problems  Selecting appropriate data structures  Thinking about the efficiency and performance  Implementing step-by-step  Testing the nominal case, border cases and efficiency
  • 56.  Software University – High-Quality Education and Employment Opportunities  softuni.bg  Software University Foundation  http://softuni.foundation/  Software University @ Facebook  facebook.com/SoftwareUniversity  Software University Forums  forum.softuni.bg Trainings @ Software University (SoftUni)
  • 57.  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution-NonCom mercial-ShareAlike 4.0 International" license License 57