Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2296: Design a Text Editor

In this guide, we solve Leetcode #2296 Design a Text Editor in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Design a text editor with a cursor that can do the following: Add text to where the cursor is. Delete text from where the cursor is (simulating the backspace key).

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Stack, Design, Linked List, String, Doubly-Linked List, Simulation

Intuition

The problem has a natural nested or last-in-first-out structure.

A stack lets us resolve matches in the correct order as we scan.

Approach

Push items as they appear and pop when you can finalize a decision.

The stack captures the unresolved part of the input.

Steps:

  • Push elements as you scan.
  • Pop when a rule or match is satisfied.
  • Use the stack to compute results.

Example

Input ["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"] [[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]] Output [null, null, 4, null, "etpractice", "leet", 4, "", "practi"] Explanation TextEditor textEditor = new TextEditor(); // The current text is "|". (The '|' character represents the cursor) textEditor.addText("leetcode"); // The current text is "leetcode|". textEditor.deleteText(4); // return 4 // The current text is "leet|". // 4 characters were deleted. textEditor.addText("practice"); // The current text is "leetpractice|". textEditor.cursorRight(3); // return "etpractice" // The current text is "leetpractice|". // The cursor cannot be moved beyond the actual text and thus did not move. // "etpractice" is the last 10 characters to the left of the cursor. textEditor.cursorLeft(8); // return "leet" // The current text is "leet|practice". // "leet" is the last min(10, 4) = 4 characters to the left of the cursor. textEditor.deleteText(10); // return 4 // The current text is "|practice". // Only 4 characters were deleted. textEditor.cursorLeft(2); // return "" // The current text is "|practice". // The cursor cannot be moved beyond the actual text and thus did not move. // "" is the last min(10, 0) = 0 characters to the left of the cursor. textEditor.cursorRight(6); // return "practi" // The current text is "practi|ce". // "practi" is the last min(10, 6) = 6 characters to the left of the cursor.

Python Solution

class TextEditor: def __init__(self): self.left = [] self.right = [] def addText(self, text: str) -> None: self.left.extend(list(text)) def deleteText(self, k: int) -> int: k = min(k, len(self.left)) for _ in range(k): self.left.pop() return k def cursorLeft(self, k: int) -> str: k = min(k, len(self.left)) for _ in range(k): self.right.append(self.left.pop()) return ''.join(self.left[-10:]) def cursorRight(self, k: int) -> str: k = min(k, len(self.right)) for _ in range(k): self.left.append(self.right.pop()) return ''.join(self.left[-10:]) # Your TextEditor object will be instantiated and called as such: # obj = TextEditor() # obj.addText(text) # param_2 = obj.deleteText(k) # param_3 = obj.cursorLeft(k) # param_4 = obj.cursorRight(k)

Complexity

The time complexity is O(∣text∣)O(|\text{text}|)O(∣text∣). The space complexity is O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy