๐ Coding Interview Questions with Answers โ Part 1
๐ง 1. What is an array and how is it stored in memory?
An array is a data structure used to store multiple elements of the same data type in a contiguous block of memory.
Example: arr = [10, 20, 30, 40]
๐น Key Features
- Fixed size (in most languages)
- Fast access using index
- Stores elements sequentially
๐น Memory Representation
If an integer takes 4 bytes:
Index | Value | Memory Address
0 | 10 | 1000
1 | 20 | 1004
2 | 30 | 1008
3 | 40 | 1012
Each element is stored next to the previous one.
๐น Time Complexity
Operation | Complexity
Access | O(1)
Search | O(n)
Insert/Delete (middle) | O(n)
๐น Interview Tip
Arrays are preferred when:
- Fast indexing is needed
- Memory efficiency matters
- Data size is mostly fixed
๐ 2. What is the difference between an array and a linked list?
Feature | Array | Linked List
Memory | Contiguous | Non-contiguous
Access Speed | O(1) | O(n)
Insert/Delete | Slow | Fast
Size | Fixed | Dynamic
Extra Memory | Less | More (pointer storage)
๐น Array Example: arr = [1, 2, 3]
๐น Linked List Example: 1 โ 2 โ 3 โ NULL
Each node stores: Data + Pointer to next node
๐น When to Use
โ
Use Arrays: Random access needed, Cache-friendly operations
โ
Use Linked Lists: Frequent insertions/deletions, Dynamic memory allocation
๐น Interview Tip
Linked lists solve resizing problems of arrays but sacrifice fast access speed.
๐ 3. Explain time complexity using Big-O notation
Big-O notation measures how an algorithm grows as input size increases.
๐น Common Complexities
Complexity | Meaning
O(1) | Constant
O(log n) | Logarithmic
O(n) | Linear
O(n log n) | Efficient sorting
O(nยฒ) | Nested loops
O(2โฟ) | Exponential
๐น Example:
for i in range(n):
print(i)
This runs n times. โก๏ธ Complexity = O(n)
๐น Nested Loop Example:
for i in range(n):
for j in range(n):
print(i, j)
โก๏ธ Complexity = O(nยฒ)
๐น Why It Matters
Interviewers use Big-O to evaluate: Scalability, Efficiency, Optimization skills
๐น Interview Tip
Always discuss: Time complexity, Space complexity, Trade-offs
๐ 4. How do you implement a stack using an array?
A stack follows the LIFO principle: Last In, First Out
Operations: Push, Pop, Peek
๐น Python Implementation:
class Stack:
def init(self):
self.stack = []
def push(self, value):
self.stack.append(value)
def pop(self):
if self.is_empty():
return "Stack Underflow"
return self.stack.pop()
def peek(self):
if self.is_empty():
return None
return self.stack[-1]
def is_empty(self):
return len(self.stack) == 0
๐น Example:
s = Stack()
s.push(10)
s.push(20)
print(s.pop()) # 20
๐น Complexity
Operation | Complexity
Push | O(1)
Pop | O(1)
Peek | O(1)
๐น Real-World Uses
Undo feature, Browser history, Function call stack, Expression evaluation
๐ 5. How do you implement a queue using an array or linked list?
A queue follows the FIFO principle: First In, First Out
Operations: Enqueue, Dequeue
๐น Queue Using Array:
class Queue:
def init(self):
self.queue = []
def enqueue(self, value):
self.queue.append(value)
def dequeue(self):
if not self.queue:
return "Empty Queue"
return self.queue.pop(0)
โ ๏ธ Problem: pop(0) takes O(n) because elements shift.
๐น Queue Using Linked List:
from collections import deque
q = deque()
q.append(10)
q.append(20)
print(q.popleft())
๐น Complexity
Operation | Complexity
Enqueue | O(1)
Dequeue | O(1)
๐น Real-World Uses
CPU scheduling, Task queues, Messaging systems, BFS traversal
๐ 6. How does a hash table work?
A hash table stores key-value pairs using a hash function.
๐น Example:
student = {
"name": "John",
"age": 22
}
๐น Working:
1. Key goes into hash function
2. Hash function generates index
3. Value stored at that index
๐น Example Flow
hash("age") โ index 5
Store: table[5] = 22
Post #2951
1.03K
- โค 1