Flipkart Runway: Coding Round Preparation
Python | Beginner Level | Topic-wise Guide
1. Arrays
Arrays store elements in a contiguous memory location, allowing fast access. Common operations
include traversal, insertion, deletion, searching, and sorting.
Example Problem: Find the Maximum Element in an Array
Input: [3, 1, 5, 9, 2]
Output: 9
Solution (Python):
def find_max(arr):
return max(arr)
arr = [3, 1, 5, 9, 2]
print(find_max(arr))
2. Strings
Strings are sequences of characters. Operations include concatenation, slicing, reversing, and
checking substrings.
Example Problem: Reverse a String
Input: 'hello'
Output: 'olleh'
Solution (Python):
def reverse_string(s):
return s[::-1]
print(reverse_string('hello'))
3. Linked Lists
A Linked List is a linear data structure where each element points to the next. Common operations
include traversal, insertion, deletion, and reversal.
Example Problem: Reverse a Linked List
Input: 1 -> 2 -> 3 -> 4
Output: 4 -> 3 -> 2 -> 1
Solution (Python):
class Node:
def __init__(self, data):
self.data = data
self.next = None
def reverse(head):
prev, curr = None, head
while curr:
nxt = curr.next
curr.next = prev
prev, curr = curr, nxt
return prev
4. Stacks & Queues
A stack follows Last In First Out (LIFO), while a queue follows First In First Out (FIFO). Stacks are
used in recursion, backtracking, and expression evaluation.
Example Problem: Implement a Stack using a List
Operations: push(1), push(2), pop() -> Returns 2
Solution (Python):
class Stack:
def __init__(self):
self.stack = []
def push(self, x):
self.stack.append(x)
def pop(self):
return self.stack.pop() if self.stack else None
s = Stack()
s.push(1)
s.push(2)
print(s.pop())
5. Recursion & Backtracking
Recursion is a function calling itself to solve subproblems. Backtracking explores possible solutions
recursively.
Example Problem: Compute Factorial using Recursion
Input: 5
Output: 120
Solution (Python):
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
print(factorial(5))
6. Sorting & Searching
Sorting algorithms include Bubble Sort, Quick Sort, Merge Sort, etc. Searching includes Binary
Search for fast lookup.
Example Problem: Binary Search
Input: [1, 3, 5, 7, 9], target=5
Output: Found at index 2
Solution (Python):
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
print(binary_search([1,3,5,7,9], 5))
7. Dynamic Programming
Dynamic Programming (DP) solves problems by breaking them into smaller subproblems and
storing results.
Example Problem: Fibonacci Sequence using DP
Input: 6
Output: 8
Solution (Python):
def fibonacci(n, memo={}):
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)
return memo[n]
print(fibonacci(6))
8. Graphs & Trees
Graphs represent relationships between nodes. Trees are a special form of graphs used in
hierarchical data storage.
Example Problem: BFS Traversal of a Graph
Input: Graph with edges [(0,1), (0,2), (1,3), (2,3)]
Output: 0 -> 1 -> 2 -> 3
Solution (Python):
from collections import deque
def bfs(graph, start):
queue = deque([start])
visited = set()
while queue:
node = queue.popleft()
if node not in visited:
print(node, end=' ')
visited.add(node)
queue.extend(graph[node])
graph = {0: [1, 2], 1: [3], 2: [3], 3: []}
bfs(graph, 0)