KatInterview

Topic

Stacks & Queues interview problems

Monotonic stacks, parsing and simulation. 112 original problems, each with hidden test cases you can run in the browser.

Problems112

22 easy74 medium16 hard

Showing 31–60 of 112 results

  • Solve two streaming state problems: daily stock trading based on sign patterns in O(N) time and two-character substring elimination using an in-place stack.

    Model answerstacks-queuesarraysstringsmath-bits
  • Write an interpreter that parses a line-based program of LOAD, STORE and integer arithmetic instructions, runs it on an operand stack, and returns the final variable memory.

    Model answerinterpreterstackparsingerror-handling
  • Decide whether a string of three kinds of brackets is correctly nested, using a stack to enforce that the most recently opened bracket is closed first.

    Auto-gradedstackstringmatchingparsing
  • Recursively or iteratively check if two arbitrarily nested data structures are identical in type, shape, and value.

    Auto-gradedrecursionstackdata-structureshashing
  • Tests using a stack to repeatedly cancel adjacent equal characters in a string, including cascading cancellations, in linear time.

    Auto-gradedstackstringsimulation
  • Sweep a single-threaded call trace with a stack to total each function's self time under half-open intervals, with recursion, strict validation, and name-sorted output.

    Auto-gradedstacksimulationhash-mapvalidation
  • Simulate cascading deletions of equal neighbouring letters with a stack, either cancelling pairs or erasing whole leftmost runs, and return what is left.

    Auto-gradedstackstringsimulationrun-length
  • Simulate the scheduling of access requests through a shared resource using state tracking and queues under conflict-resolution rules.

    Auto-gradedstacks-queuessimulationarrays
  • Parse a string with nested count-and-bracket repeat markers and expand it fully, testing recursive or stack-based parsing and careful handling of multi-digit counts.

    Auto-gradedstringsstackrecursionparsing
  • Simulate a small stack machine over whitespace-separated tokens, handling arithmetic, DUP, POP and PRINT, and stop at the first error while keeping the output produced so far.

    Model answerstacksimulationparsingerror-handling
  • Implement a concurrent, room-based TCP chat server in Python using asyncio, featuring command processing, scoped broadcasts, backpressure handling, and strict resource cleanup.

    Model answerconcurrencyasyncionetworkingpython
  • Find the length of the longest contiguous well-formed stretch in a string of only round brackets, testing stack-based index tracking and handling of unmatched characters.

    Auto-gradedstackstringparenthesestwo-pass
  • Implement two production-ready string parsers: an arithmetic evaluator supporting basic operators and precedence, and a nested string expansion utility.

    Model answerstackparsingstringsexpression-evaluation
  • Parse a parenthesized pre-order tree string with a stack and undo a letter-substitution cipher, where each node's text is the run just before its closing parenthesis.

    Auto-gradedstackstring-parsingtree-deserializationsubstitution-cipher
  • Simulate a sequence of stack commands and answer minimum queries in constant time by tracking the running minimum alongside each element.

    Auto-gradedstackdesignsimulationauxiliary-stack
  • Evaluate tracking a sliding window over incoming numbers to incrementally update and output moving averages.

    Auto-gradedsliding-windowqueuedequearray
  • Count how many rounds of simultaneous removals occur in a row of numbers where each element larger than its left neighbor is removed, using a monotonic stack to beat quadratic simulation.

    Auto-gradedmonotonic-stacksimulationarraysamortized-analysis
  • Find the longest subarray whose two end values are each strictly larger than everything between them, using a monotonic stack to avoid checking all pairs.

    Auto-gradedmonotonic-stackarraysnearest-greateramortized-analysis
  • Diff consecutive call-stack snapshots by longest common prefix to produce correctly ordered start and end events, including recursion, empty stacks and final closing.

    Auto-gradedstacksimulationlongest-common-prefixarrays
  • Scan a string and delete each block of k equal adjacent characters as soon as it forms, handling cascading merges with a run-length stack.

    Auto-gradedstackstringrun-lengthsimulation
  • Parse and evaluate a parenthesized prefix language with sum, prod and set, handling lexical scoping, strict validation and 32-bit overflow rules.

    Auto-gradedparsingrecursionscopingstack
  • Simulate a one-worker FIFO queue with a capped waiting line and report each job's finish time or -1 if it was rejected, using a deque of finish times.

    Auto-gradedqueuedequesimulationamortized-analysis
  • Use a stack to turn start/end trace records of nested calls on one CPU into each function's self-time, with inclusive end timestamps.

    Auto-gradedstackparsingsimulationstrings
  • Find, for each element, the nearest later value that is less than or equal to it and subtract it, which tests monotonic stack reasoning on arrays.

    Auto-gradedmonotonic-stackarraysnext-smaller-element
  • Compute the largest rectangle that fits under a row of unit-width columns, testing monotonic stack usage and boundary reasoning over contiguous ranges.

    Auto-gradedmonotonic-stackarrayhistogramcontiguous-range
  • Turn a flat sequence of (depth, label) entries into a nested tree by attaching each entry to its nearest earlier entry with a smaller depth, using a monotonic stack.

    Auto-gradedstacktree-constructionparsingmonotonic-stack
  • Flatten an arbitrarily nested Python list into a single list in left-to-right order, treating only lists as containers and handling very deep nesting safely.

    Auto-gradedstackiterationnested-listsdepth-first-search
  • Work out the print order of a short script mixing timers, promises and microtasks, then explain how the browser event loop schedules each kind of task.

    Model answerjavascriptevent-loopasyncmicrotasks
  • Parse and evaluate an infix arithmetic string, rejecting it with False if any intermediate result is zero, negative, or a non-integer quotient.

    Model answerparsingrecursive-descentexpression-evaluationvalidation
  • Decide whether a string of three bracket kinds is balanced, testing last-in-first-out matching with a stack and handling crossings and leftover or stray brackets.

    Auto-gradedstackstringsparsingbracket-matching

More topics