List creation in R Programming - Time & Space Complexity
When we create a list in R, we want to know how the time to build it changes as the list gets bigger.
We ask: How does the work grow when we add more items to the list?
Analyze the time complexity of the following code snippet.
my_list <- list()
for (i in 1:n) {
my_list[[i]] <- i * 2
}
This code creates a list and fills it with numbers doubled from 1 to n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding one item to the list inside the loop.
- How many times: Exactly n times, once for each number from 1 to n.
Each new item means one more step, so the work grows steadily as n grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The time grows directly with the number of items added.
Time Complexity: O(n)
This means the time to create the list grows in a straight line as the list gets bigger.
[X] Wrong: "Adding items to a list is instant and does not depend on size."
[OK] Correct: Each new item requires a step, so more items mean more time.
Understanding how list creation time grows helps you explain efficiency clearly in interviews.
What if we pre-allocate the list size before the loop? How would the time complexity change?
Practice
Solution
Step 1: Understand list creation in R
In R, lists are created using thelist()function.Step 2: Identify the correct syntax
The syntaxmy_list <- list()creates an empty list. Other options use incorrect syntax for lists.Final Answer:
my_list <- list() -> Option BQuick Check:
list() creates an empty list [OK]
- Using [] which is for vectors, not lists
- Using {} which is for expressions or sets in other languages
- Using array() which creates arrays, not lists
my_list in R?Solution
Step 1: Understand list indexing in R
Lists in R use double brackets [[ ]] to access or assign elements.Step 2: Add element at new position
To add an element at the end, assign tomy_list[[length(my_list) + 1]]. Single brackets return sublists, not elements.Final Answer:
my_list[[length(my_list) + 1]] <- 5 -> Option CQuick Check:
Use double brackets [[ ]] to add list elements [OK]
- Using single brackets [] which return sublists
- Assigning to existing last element index instead of length+1
- Confusing vector indexing with list indexing
my_list <- list(10, "apple", TRUE) my_list[[length(my_list) + 1]] <- 3.14 my_list
Solution
Step 1: Initial list creation
The listmy_liststarts with three elements: 10, "apple", and TRUE.Step 2: Adding new element at the end
Usingmy_list[[length(my_list) + 1]] <- 3.14adds 3.14 as the fourth element.Final Answer:
list(10, "apple", TRUE, 3.14) -> Option AQuick Check:
Adding element extends list length by 1 [OK]
- Expecting no change to list
- Thinking new element is nested list
- Confusing single and double brackets
my_list <- list(1, 2, 3) my_list[length(my_list) + 1] <- 4
Solution
Step 1: Understand list element assignment
In R, list elements must be assigned with double brackets [[ ]] to assign the element itself.Step 2: Identify the mistake
The code uses single brackets [] which assign a sublist, causing unexpected behavior or errors.Final Answer:
Using single brackets [] instead of double brackets [[ ]] for list element assignment -> Option AQuick Check:
Use [[ ]] to assign list elements [OK]
- Using [] which returns sublists, not elements
- Thinking length+1 is wrong index
- Assuming assigning number is invalid
Solution
Step 1: Understand list elements types
The list should contain a number, a character vector, and a nested list.Step 2: Check syntax for each element
Character vectors usec(), lists uselist(). Square brackets [] are invalid in R for these purposes.Step 3: Verify correct code
my_list <- list(42, c("a", "b"), list(TRUE, FALSE)) correctly useslist(42, c("a", "b"), list(TRUE, FALSE)).Final Answer:
my_list <- list(42, c("a", "b"), list(TRUE, FALSE)) -> Option DQuick Check:
Use c() for vectors, list() for nested lists [OK]
- Using square brackets [] which are invalid in R
- Confusing vectors and lists syntax
- Not nesting lists properly
Ready to prove it?
Pass the test and this pattern moves unknown
