Skip to content

Latest commit

 

History

History
515 lines (351 loc) · 10.4 KB

File metadata and controls

515 lines (351 loc) · 10.4 KB

PYTHON LOGIC & PROBLEM-SOLVING — 100 PROBLEMS

LEVEL 1 — FUNDAMENTALS Problems 1–15 Goal: Variables, conditions, loops, basic functions

  1. Temperature Classifier Given a temperature, classify it as: < 0 → Freezing 0–15 → Cold 16–30 → Moderate 31–40 → Hot

    40 → Extreme Heat

  2. Number Classifier Given an integer, determine whether it is: Positive / Negative / Zero Even / Odd

  3. Largest of Three Find the largest of three integers without using max().

  4. Leap Year Determine whether a given year is a leap year.

  5. Grade Calculator Given marks for 5 subjects, calculate: Total Percentage Grade Pass/Fail

  6. Electricity Bill Calculate electricity bill using slab-based pricing.

  7. Factorial Calculate factorial without using math.factorial().

  8. Fibonacci Print the first N Fibonacci numbers.

  9. Prime Number Determine whether a number is prime.

  10. Prime Numbers in Range Print all prime numbers between A and B.

  11. Digit Counter Count the number of digits in an integer without converting it to a string.

  12. Digit Sum Calculate the sum of all digits of an integer.

  13. Reverse Number Reverse an integer mathematically.

  14. Armstrong Number Determine whether a number is an Armstrong number.

  15. GCD and LCM Calculate GCD and LCM of two integers without using math.gcd().

LEVEL 2 — STRINGS Problems 16–25 Goal: String manipulation and character logic

  1. Reverse String Reverse a string without using [::-1].

  2. Palindrome String Determine whether a string is a palindrome.

  3. Character Frequency Count the frequency of every character.

  4. First Non-Repeating Character Return the first character that appears exactly once.

  5. Anagram Checker Determine whether two strings are anagrams.

  6. Remove Duplicate Characters Remove duplicate characters while preserving original order.

  7. Longest Word Find the longest word in a sentence.

  8. Word Frequency Count how many times each word appears.

  9. String Compression Convert: aaabbccccd into: a3b2c4d1

  10. Longest Consecutive Character Find the longest sequence of the same character.

Example: aaabbccccdd Output: 4

LEVEL 3 — LISTS & ARRAYS Problems 26–40 Goal: Array manipulation and algorithmic thinking

  1. Find Maximum Find maximum element without max().

  2. Find Second Largest Find the second-largest unique number.

  3. Remove Duplicates Remove duplicates from a list without using set().

  4. Array Rotation Rotate an array right by K positions.

  5. Move Zeros Move all zeros to the end while maintaining order.

Example: [0,1,0,3,12] → [1,3,12,0,0]

  1. Two Sum Given an array and target, return indices of two numbers whose sum equals target.

  2. Three Sum Find all unique triplets whose sum is zero.

  3. Missing Number Given numbers from 0 to N with one missing, find the missing number.

  4. Duplicate Number Find the duplicate number in an array containing numbers 1–N.

  5. Intersection Find the intersection of two arrays.

  6. Union Find the union of two arrays without using set().

  7. Maximum Subarray Find the contiguous subarray with the maximum sum.

Example: [-2,1,-3,4,-1,2,1,-5,4] Output: 6

  1. Minimum Subarray Find the contiguous subarray with the minimum sum.

  2. Product Except Self For each element, calculate the product of all other elements.

Do not use division.

  1. Majority Element Find the element appearing more than N/2 times.

LEVEL 4 — HASH MAPS & FREQUENCY Problems 41–50 Goal: Dictionaries, counting, efficient lookup

  1. Two Sum Optimized Solve Two Sum using a dictionary in O(N).

  2. Frequency Sort Sort characters according to frequency.

  3. Top K Frequent Elements Return the K most frequent elements.

  4. Group Anagrams Group words that are anagrams.

Example: ["eat","tea","tan","ate","nat","bat"]

Output: [["eat","tea","ate"], ["tan","nat"], ["bat"]]

  1. First Unique Number Find the first number appearing exactly once.

  2. Duplicate Detection Determine whether any value appears more than once.

  3. Common Elements Find elements occurring in all three arrays.

  4. Subarray Sum Determine whether an array contains a subarray whose sum equals K.

  5. Longest Consecutive Sequence Find the length of the longest consecutive sequence.

  6. Isomorphic Strings Determine whether two strings follow the same character pattern.

LEVEL 5 — TWO POINTERS & SLIDING WINDOW Problems 51–60 Goal: Efficient O(N) algorithms

  1. Two Sum Sorted Given a sorted array, find two numbers that sum to target.

  2. Remove Duplicates from Sorted Array Modify the array in-place.

  3. Container With Most Water Find two lines that contain the maximum amount of water.

  4. Valid Palindrome Determine whether a string is a palindrome after removing non-alphanumeric characters.

  5. Longest Substring Without Repeating Characters Return the length of the longest substring containing unique characters.

  6. Maximum Sum Window Find maximum sum of any subarray of size K.

  7. Minimum Size Subarray Find the smallest subarray whose sum is at least K.

  8. Longest Ones Given binary array, find longest sequence of 1s after flipping at most K zeros.

  9. Permutation in String Determine whether one string contains a permutation of another.

  10. Longest Repeating Character Given a string and K replacements, find the longest substring containing one repeated character.

LEVEL 6 — STACKS & QUEUES Problems 61–70 Goal: LIFO/FIFO logic

  1. Valid Parentheses Check whether brackets are correctly balanced.

Example: "{[()]}" → Valid

  1. Min Stack Design a stack supporting: push() pop() top() get_min()

All in O(1).

  1. Evaluate Reverse Polish Notation Evaluate: ["2","1","+","3","*"]

  2. Remove Adjacent Duplicates Repeatedly remove adjacent duplicate characters.

  3. Next Greater Element For each element, find the next greater element to its right.

  4. Daily Temperatures For each day, determine how many days until a warmer temperature.

  5. Queue Using Two Stacks Implement a queue using only two stacks.

  6. Stack Using Queues Implement a stack using queues.

  7. Simplify Path Convert a Unix-style path into its canonical form.

  8. Largest Rectangle in Histogram Find the largest rectangle area in a histogram.

LEVEL 7 — RECURSION & BACKTRACKING Problems 71–80 Goal: Recursive reasoning and search

  1. Recursive Factorial Implement factorial recursively.

  2. Recursive Fibonacci Implement Fibonacci recursively and explain its complexity.

  3. Power Function Calculate x^n recursively.

  4. Generate Parentheses Generate all valid combinations of N pairs of parentheses.

  5. Subsets Generate all subsets of a list.

  6. Permutations Generate all permutations of a list.

  7. Combination Sum Find combinations that sum to a target.

  8. Letter Combinations Given phone digits, generate all possible letter combinations.

  9. Word Search Given a character grid and a word, determine whether the word exists.

  10. N-Queens Place N queens on an N×N chessboard so no two queens attack each other.

LEVEL 8 — LINKED LISTS Problems 81–85 Goal: Pointer manipulation

  1. Reverse Linked List Reverse a singly linked list.

  2. Detect Cycle Determine whether a linked list contains a cycle.

  3. Middle of Linked List Find the middle node using O(1) extra space.

  4. Merge Two Sorted Lists Merge two sorted linked lists.

  5. Remove Nth Node Remove the Nth node from the end of a linked list.

LEVEL 9 — TREES & GRAPHS Problems 86–95 Goal: Graph traversal and hierarchical data

  1. Binary Tree Inorder Traversal Return inorder traversal of a binary tree.

  2. Maximum Tree Depth Find maximum depth recursively.

  3. Level Order Traversal Return nodes level-by-level.

  4. Validate Binary Search Tree Determine whether a binary tree is a valid BST.

  5. Lowest Common Ancestor Find the lowest common ancestor of two nodes.

  6. Number of Islands Given a binary grid, count connected islands.

  7. Flood Fill Implement a flood-fill algorithm.

  8. Clone Graph Create a deep copy of a graph.

  9. Course Schedule Determine whether all courses can be completed given prerequisites.

  10. Shortest Path Find the shortest path between two nodes in an unweighted graph.

LEVEL 10 — ADVANCED / BIOINFORMATICS LOGIC Problems 96–100 Goal: Apply algorithmic thinking to biotechnology

  1. DNA Complement

Given: ATGCCGTA

Return its complementary DNA strand: TACGGCAT

Rules: A ↔ T C ↔ G

Then implement the reverse-complement function.

Constraints: 1 <= sequence length <= 10^6

  1. DNA Mutation Distance

Given two DNA sequences of equal length, calculate the number of positions where they differ.

Example: ATGCCA ATGCTA

Output: 1

Then extend the problem to calculate percentage identity.

  1. Longest ORF

Given a DNA sequence, identify the longest open reading frame (ORF).

Start codon: ATG

Stop codons: TAA TAG TGA

Return: Start position Stop position ORF sequence ORF length

Consider all three reading frames.

  1. Protein Motif Search

Given a protein sequence and a motif pattern, determine every position where the motif occurs.

Example:

Protein: MKTLLVAGAGKTNAA

Motif: GAG

Output: Position(s): 7

Then extend the problem to support wildcard "X".

Example: GAXG

should match: GATG GACG GAGG etc.

  1. Protein Sequence Analysis Engine

Build a complete protein-analysis program.

Input: A CSV file containing multiple protein sequences.

For every sequence calculate:

  1. Sequence ID
  2. Protein length
  3. Molecular mass
  4. Amino-acid composition
  5. Percentage of hydrophobic residues
  6. Percentage of charged residues
  7. Number of cysteines
  8. Number of tryptophans
  9. Theoretical extinction coefficient
  10. Basic/acidic residue ratio
  11. Invalid amino-acid characters

Output: A new CSV file containing all calculated properties.

Additional requirements:

• Handle invalid sequences without crashing. • Process thousands of sequences. • Do not use Biopython. • Separate file handling from biological calculations. • Write reusable functions. • Use appropriate data structures. • Explain the time complexity of your algorithm. • Test your program with at least 10 sequences. • Include edge cases.

BONUS:

After completing the program, optimize it for:

Time complexity Memory usage Large input files

Target: 100,000 protein sequences Average length: 500 amino acids