Python Coding Challenges — Practice Questions
50 questions. Try each one yourself before checking the answer.
Given a sentence, build a word-frequency dictionary — the foundational piece the full analyser will build on.
Given a log line "2024-06-15 14:30:00 ERROR Failed to connect", extract just the log LEVEL using .split().
Given a single Sudoku row (a list of 9 digits, 0 for empty), check whether it has any DUPLICATE non-zero digits.
Store a single password entry in a dict keyed by service name, then retrieve it.
Given a CSV file, read just the HEADER row using csv.reader().
Add a single item with a quantity to an inventory dictionary.
Given a filename, extract its extension using os.path.splitext().
Given frequency = {"the": 2, "cat": 1, "mat": 1}, find the MOST common word using max().
Given a list of log lines, count how many are "ERROR" level using .startswith()-style checking on the line's content.
Given a Sudoku BOARD (list of 9 rows), extract a single COLUMN as a list, then check it for duplicates.
Read a text file's contents and build a full word-frequency dictionary from it.
Parse a log line into (timestamp, level, message) using .split(maxsplit=...).
Given a Sudoku board, extract a single 3x3 BOX (given its top-left row/col) and check it for duplicates.
Build add_password(vault, service, password) and get_password(vault, service) functions wrapping the dict access.
Given a CSV with a numeric score column, compute the average score using csv.DictReader.
Add update_quantity(inventory, item, change) that adjusts an item's stock, removing it entirely if it hits zero.
Group a folder's files into a dict keyed by extension, WITHOUT moving anything yet (just a preview report).
Build a word-frequency counter that EXCLUDES common stopwords ("the", "is", "a", "an") using a set.
Given log lines each starting with "HH:MM:SS", filter lines that fall within a given time range using string comparison.
Combine row, column, and box checks into a single is_valid_sudoku(board) — covering the ROW checks fully, with column/box logic to follow.
Generate a strong random password using random and string (revisiting the Modules & Packages module).
Validate a password's strength using re — requiring a digit, an uppercase letter, and at least 8 characters.
Find the ROW with the maximum value in a numeric column, using max() with a key.
Filter CSV rows matching a condition (score >= 90) and write them into a NEW CSV file.
Build a "low stock" report: items in inventory below a given threshold.
Given an inventory dict AND a prices dict, compute the total value of all stock on hand.
Fully organize a folder: move every file into an extension-named subfolder (a working version of Q17's preview).
Rename every file in a folder to a sequential pattern (file_1.txt, file_2.txt, ...), preserving each original extension.
Build a "top N words" report from a word-frequency dictionary using sorted().
Summarize a list of log lines into a count-per-level report (INFO, WARNING, ERROR), formatted and printed.
Implement the FULL classic is_valid_sudoku(board) — checking all rows, all columns, and all nine 3x3 boxes.
Store password HASHES instead of plaintext, using hashlib.sha256(), and verify a login attempt by hashing and comparing.
Persist a HASHED password vault to a JSON file, then reload it and verify a login attempt against the loaded data.
Report which rows in a CSV have MISSING/EMPTY values in any column.
Merge two CSVs (students.csv with name, id and scores.csv with id, score) into one combined dataset, joining on id.
Process a batch of orders, raising a custom OutOfStockError if an order requests more than what's available.
Parse a full log file and build an error-count-PER-HOUR report, extracting the hour from each timestamp.
Find potential DUPLICATE files across an entire folder tree by matching both name and size, using os.walk().
Build a frequency counter that's robust to PUNCTUATION and CASE, using re to clean the text before counting.
The following Sudoku validator ONLY checks rows, silently missing column/box duplicates, so it wrongly approves an invalid board. Find and fix the gap.
def is_valid_sudoku(board):
for row in board:
filled = [n for n in row if n != 0]
if len(filled) != len(set(filled)):
return False
return TrueBuild a full "Word Frequency Analyser": read a text file, clean punctuation/case with re, exclude stopwords, and print a top-5 report.
Build a full "Log Parser & Analyzer": parse a log file, summarize counts by level, and report the busiest hour for errors.
Build a full "Sudoku Validator" tool that not only says valid/invalid but reports the SPECIFIC violation (which row, column, or box failed first).
Build a full "Password Manager" with hashed storage, add/verify/list-services operations, all persisted to JSON, behind a menu loop.
Build a full "CSV Analyser" that computes count, sum, average, min, and max for EVERY numeric column in a CSV automatically.
Explain why passwords should NEVER be stored in plaintext (even in a "just for practice" project), and why a bare sha256 hash isn't fully production-safe either (no salt), demonstrating the gap.
Explain why a Sudoku validator's nested loops are still considered O(1) despite "looking like" an algorithm with variable complexity, tying it back to the Algorithms module's Big-O discussion.
Explain why a CSV analyser should handle ONE bad row gracefully (skip and log it) rather than crashing the entire analysis, demonstrating with per-row try/except.
Explain the "dry-run mode" pattern for a File Organiser (or any destructive tool): print what WOULD happen before actually doing it, demonstrating a dry_run flag.
Build a "Coding Challenges Mastery Report" — the grand-finale demonstration, tying together the ENTIRE 25-topic curriculum. Combine mini versions of a Word Frequency Analyser, a Log Parser, and an Inventory System into one script, using OOP, file I/O, exceptions, collections, and string processing together, printing one final unified report.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session