Back to Practice Dashboard
DSA SectionEasy

Insertions

Learn how to solve the 'Insertions' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function insert_node(arr, val, pos) that represents a linked list as a Python list arr. Insert an integer val at the 0-based index pos (where pos ranges from 0 to the current length of the list) and return the updated list.

Constraints
  • 0 <= len(arr) <= 1000
  • 0 <= pos <= len(arr)
  • -10^4 <= val <= 10^4

Examples

Example 1
Input
arr = [1, 2, 3], val = 5, pos = 1
Output
[1, 5, 2, 3]
Explanation

We insert the value 5 at index 1, shifting 2 and 3 to the right.

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor