Problem solving + Pseudocode Flashcards
(30 cards)
What is a good first step when solving a new programming problem?
Understand the inputs and desired outputs, then break the problem into smaller steps.
What does ‘decomposition’ mean in programming?
Breaking a problem into smaller, more manageable sub-problems.
What does ‘divide and conquer’ mean?
A technique where a problem is divided into smaller parts, each solved recursively, and combined.
Give an example of divide and conquer.
Merge Sort: divide the array, sort each half, merge them back together.
Why is recursion often used in divide and conquer?
Because each subproblem can be solved with the same method, reducing code repetition.
How would you write pseudocode to reverse a string?
function reverse(s): result = “”, for i from length(s)-1 to 0: result += s[i], return result
How would you find the largest number in an array (pseudocode)?
function max(arr): largest = arr[0], for each item in arr: if item > largest then largest = item, return largest
Write pseudocode for a linear search.
function linearSearch(arr, target): for each item in arr: if item == target: return true, return false
Write pseudocode for counting how many times a value appears in a list.
function countOccurrences(arr, value): count = 0, for item in arr: if item == value then count += 1, return count
Why is it useful to write pseudocode before coding?
It helps plan logic without worrying about syntax, making the code easier to write and debug.
How do you trace a recursive function by hand?
Use a call stack: track each function call and return value step by step.
What’s the purpose of base cases in recursion?
They stop the recursion to prevent infinite loops and return final results.
What are the three parts of a recursive function?
Base case, recursive call, and a step that moves toward the base case.
Describe the problem-solving pattern: Input → Process → Output.
It means: Read inputs → Perform operations → Return/display results.
Write pseudocode for checking if a number is even.
function isEven(n): return n % 2 == 0
What is an algorithm?
A finite set of steps that solve a problem or complete a task.
How would you break down the problem ‘Find the average of a list of numbers’?
Add all numbers, divide by the number of items, return result.
What is dry-run testing?
Manually walking through code or pseudocode to track variable values and ensure correctness.
How can you simplify a complex problem?
Identify repeated patterns, use functions, or split into subproblems.
What is an example of a brute-force approach?
Trying all possible combinations, such as checking every pair of numbers for a sum.
Write pseudocode to count how many even numbers are in a list.
function countEvens(arr): count = 0, for each num in arr: if num % 2 == 0 then count += 1, return count
Write pseudocode to check if a list is sorted in ascending order.
function isSorted(arr): for i from 0 to length(arr)-2: if arr[i] > arr[i+1] then return false, return true
Write pseudocode to calculate the sum of all numbers in a list.
function sumList(arr): total = 0, for each num in arr: total += num, return total
Write pseudocode to reverse the order of a list.
function reverseList(arr): result = [], for i from length(arr)-1 to 0: result.append(arr[i]), return result