๐งฉNow, Let's Understand Functions in Programming ๐จโ๐ป๐ฅ
After variables, operators, conditions, and loops, the next important concept is functions.
Functions help you organize code, avoid repetition, and make programs easier to understand and maintain.
๐ง 1. What is a Function?
A function is a reusable block of code designed to perform a specific task.
Instead of writing the same code repeatedly, you can put it inside a function and call it whenever you need it.
For example:
Output:
๐ฏ 2. Why Do We Use Functions?
Functions help with:
โ Code reusability
โ Better organization
โ Easier debugging
โ Readability
โ Maintaining large programs
A good function usually focuses on one clear task.
๐ฅ 3. Function Parameters
Functions can accept information from the code that calls them.
Output:
Here, "name" is a parameter.
"Alex" is the argument passed to the function.
๐ค 4. Returning a Value
A function can return a result instead of directly displaying it.
Output:
"return" sends the result back to the place where the function was called.
๐ 5. Parameters vs Arguments
These two terms are often confused.
In:
"a" and "b" are parameters.
In:
"10" and "5" are arguments.
Parameter โ Variable defined by the function
Argument โ Actual value passed to the function
โ๏ธ 6. Default Parameters
A function can have a default value for a parameter.
Output:
If no argument is provided, the default value is used.
๐ 7. Functions in Different Languages
The syntax changes between languages, but the concept remains similar.
Python
JavaScript
Java
C++
The purpose is the same: define reusable logic that can be called when needed.
๐ง 8. Local Variables
A variable created inside a function is generally local to that function.
Here, "result" belongs to the function's local scope.
Understanding scope becomes especially important as programs become larger.
๐ 9. Functions Can Call Other Functions
One function can use another function.
This allows larger programs to be broken into smaller, manageable pieces.
๐งฉ 10. Functions and Loops
Functions and loops are often used together.
The function organizes the task, while the loop handles repetition.
๐ฅ Double Tap โค๏ธ For More
After variables, operators, conditions, and loops, the next important concept is functions.
Functions help you organize code, avoid repetition, and make programs easier to understand and maintain.
๐ง 1. What is a Function?
A function is a reusable block of code designed to perform a specific task.
Instead of writing the same code repeatedly, you can put it inside a function and call it whenever you need it.
For example:
def greet():
print("Hello!")
greet()
Output:
Hello!
๐ฏ 2. Why Do We Use Functions?
Functions help with:
โ Code reusability
โ Better organization
โ Easier debugging
โ Readability
โ Maintaining large programs
A good function usually focuses on one clear task.
๐ฅ 3. Function Parameters
Functions can accept information from the code that calls them.
def greet(name):
print("Hello", name)
greet("Alex")
Output:
Hello Alex
Here, "name" is a parameter.
"Alex" is the argument passed to the function.
๐ค 4. Returning a Value
A function can return a result instead of directly displaying it.
def add(a, b):
return a + b
result = add(10, 5)
print(result)
Output:
15
"return" sends the result back to the place where the function was called.
๐ 5. Parameters vs Arguments
These two terms are often confused.
In:
def add(a, b):
return a + b
"a" and "b" are parameters.
In:
add(10, 5)
"10" and "5" are arguments.
Parameter โ Variable defined by the function
Argument โ Actual value passed to the function
โ๏ธ 6. Default Parameters
A function can have a default value for a parameter.
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Alex")
Output:
Hello Guest
Hello Alex
If no argument is provided, the default value is used.
๐ 7. Functions in Different Languages
The syntax changes between languages, but the concept remains similar.
Python
def add(a, b):
return a + b
JavaScript
function add(a, b) {
return a + b;
}Java
static int add(int a, int b) {
return a + b;
}C++
int add(int a, int b) {
return a + b;
}The purpose is the same: define reusable logic that can be called when needed.
๐ง 8. Local Variables
A variable created inside a function is generally local to that function.
def calculate():
result = 100
print(result)
calculate()
Here, "result" belongs to the function's local scope.
Understanding scope becomes especially important as programs become larger.
๐ 9. Functions Can Call Other Functions
One function can use another function.
def add(a, b):
return a + b
def show_result():
result = add(10, 20)
print(result)
show_result()
This allows larger programs to be broken into smaller, manageable pieces.
๐งฉ 10. Functions and Loops
Functions and loops are often used together.
def print_numbers():
for i in range(1, 6):
print(i)
print_numbers()
The function organizes the task, while the loop handles repetition.
๐ฅ Double Tap โค๏ธ For More
โค13
๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ๐๐ผ๐ณ๐ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ถ๐๐ต ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ฒ๐! ๐๐ฅ
Upgrade your resume with Microsoft learning opportunities. Explore courses covering some of today's most in-demand tech skills!
๐ฏ Perfect for Students โข Freshers โข Job Seekers โข Working Professionals
โ Learn at your own pace โ Build career-relevant skills โ Explore available certificates and credentials
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ๐๐ผ๐ณ๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ฒ๐ฟ๐ฒ ๐
https://pdlink.in/3Uf5trR
๐ฅ Share with friends looking to upskill in 2026!
Upgrade your resume with Microsoft learning opportunities. Explore courses covering some of today's most in-demand tech skills!
๐ฏ Perfect for Students โข Freshers โข Job Seekers โข Working Professionals
โ Learn at your own pace โ Build career-relevant skills โ Explore available certificates and credentials
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐ ๐ถ๐ฐ๐ฟ๐ผ๐๐ผ๐ณ๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ฒ๐ฟ๐ฒ ๐
https://pdlink.in/3Uf5trR
๐ฅ Share with friends looking to upskill in 2026!
๐ฎ๐ณ ๐๐ข๐ฉ๐๐ฅ๐ก๐ ๐๐ก๐ง ๐ข๐ ๐๐ก๐๐๐ โ ๐๐๐๐ง๐ ๐๐ก๐ง๐๐ฅ๐ก๐ฆ๐๐๐ฃ๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ ๐
Students and freshers can explore internship opportunities through the AICTE Internship Portal across multiple domains:
๐ป AI, Data Science & Web Development
๐ Digital Marketing & Business Development
๐ฐ Finance & Accounting
โ๏ธ Content Writing & Graphic Design
โ๏ธ Mechanical, Civil & Electrical Engineering
๐ ๐๐ต๐ฒ๐ฐ๐ธ ๐ข๐ฝ๐ฝ๐ผ๐ฟ๐๐๐ป๐ถ๐๐ถ๐ฒ๐ & ๐๐ฝ๐ฝ๐น๐ ๐
https://pdlink.in/4y3vcBl
๐ข Share this opportunity with your friends and classmates!
Students and freshers can explore internship opportunities through the AICTE Internship Portal across multiple domains:
๐ป AI, Data Science & Web Development
๐ Digital Marketing & Business Development
๐ฐ Finance & Accounting
โ๏ธ Content Writing & Graphic Design
โ๏ธ Mechanical, Civil & Electrical Engineering
๐ ๐๐ต๐ฒ๐ฐ๐ธ ๐ข๐ฝ๐ฝ๐ผ๐ฟ๐๐๐ป๐ถ๐๐ถ๐ฒ๐ & ๐๐ฝ๐ฝ๐น๐ ๐
https://pdlink.in/4y3vcBl
๐ข Share this opportunity with your friends and classmates!
โค2
๐ ๐ช๐ต๐ถ๐ฐ๐ต ๐ง๐ฒ๐ฐ๐ต ๐ฆ๐ธ๐ถ๐น๐น ๐ช๐ถ๐น๐น ๐ฌ๐ผ๐ ๐ ๐ฎ๐๐๐ฒ๐ฟ ๐ถ๐ป ๐ฎ๐ฌ๐ฎ๐ฒ?
Want to build apps, uncover insights, or create AI solutions? Explore certification courses that match your career goals! ๐ฏ
๐ป ๐๐๐น๐น ๐ฆ๐๐ฎ๐ฐ๐ธ ๐๐ฒ๐๐ฒ๐น๐ผ๐ฝ๐บ๐ฒ๐ป๐
๐ https://pdlink.in/3SuUeuD
๐ ๐๐ฎ๐๐ฎ ๐๐ป๐ฎ๐น๐๐๐ถ๐ฐ๐
๐ https://pdlink.in/45vk5ph
๐ซ๐๐ ๐๐ป๐ด๐ถ๐ป๐ฒ๐ฒ๐ฟ๐ถ๐ป๐ด
๐ https://pdlink.in/4fWJVID
๐ฅ Choose your path, sharpen your skills, and take the next step towards your tech career!
๐ Save this post & share it with a friend planning to upskill.
Want to build apps, uncover insights, or create AI solutions? Explore certification courses that match your career goals! ๐ฏ
๐ป ๐๐๐น๐น ๐ฆ๐๐ฎ๐ฐ๐ธ ๐๐ฒ๐๐ฒ๐น๐ผ๐ฝ๐บ๐ฒ๐ป๐
๐ https://pdlink.in/3SuUeuD
๐ ๐๐ฎ๐๐ฎ ๐๐ป๐ฎ๐น๐๐๐ถ๐ฐ๐
๐ https://pdlink.in/45vk5ph
๐ซ๐๐ ๐๐ป๐ด๐ถ๐ป๐ฒ๐ฒ๐ฟ๐ถ๐ป๐ด
๐ https://pdlink.in/4fWJVID
๐ฅ Choose your path, sharpen your skills, and take the next step towards your tech career!
๐ Save this post & share it with a friend planning to upskill.
๐ง๐ข๐ฃ ๐๐ฅ๐๐ ๐๐ข๐จ๐ฅ๐ฆ๐๐ฆ ๐ง๐ข ๐๐ข๐ข๐ฆ๐ง ๐ฌ๐ข๐จ๐ฅ ๐ฆ๐๐๐๐๐ฆ ๐๐ก ๐ฎ๐ฌ๐ฎ๐ฒ!
Explore FREE online learning resources to develop in-demand skills and improve your career opportunities!
๐ Why Explore These Courses?
โ Learn valuable professional skills
โ Improve your resume
โ Build practical knowledge
โ Learn at your own pace
๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ฒ๐ฟ๐ฒ๐
https://pdlink.in/3VBdt6V
๐ก Learn New Skills โ Build Confidence โ Unlock Career Opportunities!
Explore FREE online learning resources to develop in-demand skills and improve your career opportunities!
๐ Why Explore These Courses?
โ Learn valuable professional skills
โ Improve your resume
โ Build practical knowledge
โ Learn at your own pace
๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ฒ๐ฟ๐ฒ๐
https://pdlink.in/3VBdt6V
๐ก Learn New Skills โ Build Confidence โ Unlock Career Opportunities!
Hey guys,
Here is the list of best curated Telegram Channels for free education ๐๐
Free Courses with Certificate
Web Development Free Resources
Data Science & Machine Learning
Programming Free Books
Python Free Courses
Python Interview Resources
Ethical Hacking & Cyber Security
English Speaking & Communication
Stock Marketing & Investment Banking
Coding Projects
Jobs & Internship Opportunities
Learn Digital Marketing
Crack your coding Interviews
Udemy Free Courses with Certificate
Earn $10000 with ChatGPT
Google Jobs
Java Programming Free Resources
Learn Blockchain & Crypto
Data Analyst Jobs
Artificial Intelligence
Free access to all the Paid Channels
๐๐
https://t.me/addlist/4q2PYC0pH_VjZDk5
Do react with โฅ๏ธ if you need more content free resources
ENJOY LEARNING ๐๐
Here is the list of best curated Telegram Channels for free education ๐๐
Free Courses with Certificate
Web Development Free Resources
Data Science & Machine Learning
Programming Free Books
Python Free Courses
Python Interview Resources
Ethical Hacking & Cyber Security
English Speaking & Communication
Stock Marketing & Investment Banking
Coding Projects
Jobs & Internship Opportunities
Learn Digital Marketing
Crack your coding Interviews
Udemy Free Courses with Certificate
Earn $10000 with ChatGPT
Google Jobs
Java Programming Free Resources
Learn Blockchain & Crypto
Data Analyst Jobs
Artificial Intelligence
Free access to all the Paid Channels
๐๐
https://t.me/addlist/4q2PYC0pH_VjZDk5
Do react with โฅ๏ธ if you need more content free resources
ENJOY LEARNING ๐๐
โค6
๐๐ฎ๐๐ฎ ๐๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป ๐ช๐ถ๐๐ต ๐๐๐
Future Ready Data Science and AI Skills You Build
->Build Python, Machine Learning, and AI Skills
->Get Mentored by Industry Experts
->Practice with Mock Interviews
->Ace Your Placements & Land Dream Tech Job
๐ค 500+ Partner Companies |๐ 2000+ Students Placed
๐ ๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐ก๐ผ๐ ๐ :-
https://pdlink.in/47pezFz
๐ฒ Save & Share with Friends Preparing for Data Science & AI Jobs!
Future Ready Data Science and AI Skills You Build
->Build Python, Machine Learning, and AI Skills
->Get Mentored by Industry Experts
->Practice with Mock Interviews
->Ace Your Placements & Land Dream Tech Job
๐ค 500+ Partner Companies |๐ 2000+ Students Placed
๐ ๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐ก๐ผ๐ ๐ :-
https://pdlink.in/47pezFz
๐ฒ Save & Share with Friends Preparing for Data Science & AI Jobs!
โค2
๐๐๐ ๐๐ฅ๐๐ ๐ง๐๐๐ ๐๐ข๐จ๐ฅ๐ฆ๐๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ! ๐
Explore IBM Cognitive Class and build in-demand tech skills without spending money on course fees!
๐ ๐ง๐ผ๐ฝ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ผ ๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ:-
๐ซ Artificial Intelligence & Machine Learning
๐ฅPython for Data Science
๐ Data Science Fundamentals
โ๏ธ Cloud Computing & Big Data
๐ ๐ฆ๐๐ฎ๐ฟ๐ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด ๐๐ฒ๐ฟ๐ฒ ๐
https://pdlink.in/4zGswuX
๐ฒ Save & Share with friends preparing for tech careers! ๐ฅ
Explore IBM Cognitive Class and build in-demand tech skills without spending money on course fees!
๐ ๐ง๐ผ๐ฝ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ผ ๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ:-
๐ซ Artificial Intelligence & Machine Learning
๐ฅPython for Data Science
๐ Data Science Fundamentals
โ๏ธ Cloud Computing & Big Data
๐ ๐ฆ๐๐ฎ๐ฟ๐ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด ๐๐ฒ๐ฟ๐ฒ ๐
https://pdlink.in/4zGswuX
๐ฒ Save & Share with friends preparing for tech careers! ๐ฅ
โค1
๐ฆ Common Array Interview Questions with Answers
After understanding the fundamentals of Arrays, let's look at the questions frequently asked in coding and technical interviews.
๐น 1. What is an Array?
Answer:
An array is a data structure used to store multiple elements in an ordered sequence.
Each element can generally be accessed using an index.
Example:
Output:
The index of "30" is "2".
The main advantage of an array is efficient index-based access.
๐น 2. What is the time complexity of accessing an array element?
Answer:
Accessing an element by its index is typically:
O(1)
Example:
The system can directly access the element at index "3" rather than searching through all previous elements.
This is one of the biggest advantages of arrays.
๐น 3. What is the difference between an Array and a Linked List?
Answer:
An array stores elements in a contiguous structure in traditional array implementations, while a linked list stores elements as separate nodes connected using references/pointers.
Arrays provide fast index-based access:
arr[i] โ O(1)
Linked lists generally require traversal to reach a particular position:
Access โ O(n)
However, linked lists can be advantageous when inserting or deleting nodes at a known position because elements do not need to be shifted like they may in an array.
The choice depends on the problem.
๐น 4. How do you find the largest element in an array?
Answer:
Traverse the array while maintaining the largest value found so far.
Example:
Output:
Time complexity:
O(n)
Space complexity:
O(1)
๐น 5. How do you find the second-largest element?
Answer:
We can maintain two variables:
largest
second_largest
Example:
Output:
This can be solved in:
Time: O(n)
Space: O(1)
An important interview follow-up is:
"What if all elements are equal?"
A good solution should explicitly handle the case where a distinct second-largest element does not exist.
๐น 6. How do you reverse an array?
Answer:
One approach is to use two pointers.
Example:
Output:
Time complexity:
O(n)
Space complexity:
O(1)
This is an example of an in-place algorithm because we modify the original array instead of creating another array.
๐น 7. How do you find duplicate elements in an array?
Answer:
A HashSet can be used to keep track of elements already encountered.
Example:
Output:
After understanding the fundamentals of Arrays, let's look at the questions frequently asked in coding and technical interviews.
๐น 1. What is an Array?
Answer:
An array is a data structure used to store multiple elements in an ordered sequence.
Each element can generally be accessed using an index.
Example:
arr = [10, 20, 30, 40]
print(arr[2])
Output:
30
The index of "30" is "2".
The main advantage of an array is efficient index-based access.
๐น 2. What is the time complexity of accessing an array element?
Answer:
Accessing an element by its index is typically:
O(1)
Example:
arr = [10, 20, 30, 40, 50]
print(arr[3])
The system can directly access the element at index "3" rather than searching through all previous elements.
This is one of the biggest advantages of arrays.
๐น 3. What is the difference between an Array and a Linked List?
Answer:
An array stores elements in a contiguous structure in traditional array implementations, while a linked list stores elements as separate nodes connected using references/pointers.
Arrays provide fast index-based access:
arr[i] โ O(1)
Linked lists generally require traversal to reach a particular position:
Access โ O(n)
However, linked lists can be advantageous when inserting or deleting nodes at a known position because elements do not need to be shifted like they may in an array.
The choice depends on the problem.
๐น 4. How do you find the largest element in an array?
Answer:
Traverse the array while maintaining the largest value found so far.
Example:
arr = [10, 45, 23, 67, 12]
largest = arr[0]
for num in arr:
if num > largest:
largest = num
print(largest)
Output:
67
Time complexity:
O(n)
Space complexity:
O(1)
๐น 5. How do you find the second-largest element?
Answer:
We can maintain two variables:
largest
second_largest
Example:
arr = [10, 45, 23, 67, 12]
largest = float('-inf')
second_largest = float('-inf')
for num in arr:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
print(second_largest)
Output:
45
This can be solved in:
Time: O(n)
Space: O(1)
An important interview follow-up is:
"What if all elements are equal?"
A good solution should explicitly handle the case where a distinct second-largest element does not exist.
๐น 6. How do you reverse an array?
Answer:
One approach is to use two pointers.
Example:
arr = [1, 2, 3, 4, 5]
left = 0
right = len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
print(arr)
Output:
[5, 4, 3, 2, 1]
Time complexity:
O(n)
Space complexity:
O(1)
This is an example of an in-place algorithm because we modify the original array instead of creating another array.
๐น 7. How do you find duplicate elements in an array?
Answer:
A HashSet can be used to keep track of elements already encountered.
Example:
arr = [1, 2, 3, 2, 4, 1]
seen = set()
duplicates = set()
for num in arr:
if num in seen:
duplicates.add(num)
else:
seen.add(num)
print(duplicates)
Output:
{1, 2}โค2
Average time complexity:
O(n)
Additional space:
O(n)
This is usually faster than comparing every pair, which can take O(nยฒ).
๐น 8. What is the Two Sum problem?
Answer:
You are given an array and a target value. Find two elements whose sum equals the target.
Example:
Array: [2, 7, 11, 15]
Target: 9
The answer is:
2 + 7 = 9
A HashMap can solve this efficiently.
Output:
Average time complexity:
O(n)
Space complexity:
O(n)
This is one of the most important patterns to recognize in array interviews.
๐น 9. How do you move all zeroes to the end?
Example:
Input:
[0, 1, 0, 3, 12]
Output:
[1, 3, 12, 0, 0]
A Two Pointer approach can solve this in-place.
Output:
Time complexity:
O(n)
Space complexity:
O(1)
๐น 10. How do you find the missing number?
Suppose the array contains numbers from "0" to "n", but one number is missing.
Example:
[3, 0, 1]
The missing number is:
2
One efficient approach uses the expected sum:
Output:
Time complexity:
O(n)
Space complexity:
O(1)
๐น 11. What is the difference between Linear Search and Binary Search?
Answer:
Linear Search checks elements one by one.
Time: O(n)
It can work on unsorted data.
Binary Search repeatedly divides the search space approximately in half.
Time: O(log n)
It requires suitable sorted data.
For example:
Linear Search:
10 โ 20 โ 30 โ 40 โ 50
Binary Search:
30
/ \
10 40...
The key interview point is not simply memorizing the complexities. You should understand when Binary Search can actually be applied.
๐น 12. What is an in-place algorithm?
Answer:
An in-place algorithm modifies the input data structure while using only a small amount of additional memory.
For example, reversing an array using two pointers:
No second array is created.
Therefore, the extra space used is:
O(1)
๐น 13. What is the Maximum Subarray problem?
Answer:
You are given an array of integers and need to find the contiguous subarray with the maximum sum.
Example:
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
The maximum-sum contiguous subarray is:
[4, -1, 2, 1]
Its sum is:
6
A famous solution uses Kadane's Algorithm.
Output:
O(n)
Additional space:
O(n)
This is usually faster than comparing every pair, which can take O(nยฒ).
๐น 8. What is the Two Sum problem?
Answer:
You are given an array and a target value. Find two elements whose sum equals the target.
Example:
Array: [2, 7, 11, 15]
Target: 9
The answer is:
2 + 7 = 9
A HashMap can solve this efficiently.
arr = [2, 7, 11, 15]
target = 9
seen = {}
for i, num in enumerate(arr):
complement = target - num
if complement in seen:
print(seen[complement], i)
break
seen[num] = i
Output:
0 1
Average time complexity:
O(n)
Space complexity:
O(n)
This is one of the most important patterns to recognize in array interviews.
๐น 9. How do you move all zeroes to the end?
Example:
Input:
[0, 1, 0, 3, 12]
Output:
[1, 3, 12, 0, 0]
A Two Pointer approach can solve this in-place.
arr = [0, 1, 0, 3, 12]
position = 0
for i in range(len(arr)):
if arr[i] != 0:
arr[position], arr[i] = arr[i], arr[position]
position += 1
print(arr)
Output:
[1, 3, 12, 0, 0]
Time complexity:
O(n)
Space complexity:
O(1)
๐น 10. How do you find the missing number?
Suppose the array contains numbers from "0" to "n", but one number is missing.
Example:
[3, 0, 1]
The missing number is:
2
One efficient approach uses the expected sum:
arr = [3, 0, 1]
n = len(arr)
expected_sum = n * (n + 1) // 2
actual_sum = sum(arr)
missing = expected_sum - actual_sum
print(missing)
Output:
2
Time complexity:
O(n)
Space complexity:
O(1)
๐น 11. What is the difference between Linear Search and Binary Search?
Answer:
Linear Search checks elements one by one.
Time: O(n)
It can work on unsorted data.
Binary Search repeatedly divides the search space approximately in half.
Time: O(log n)
It requires suitable sorted data.
For example:
Linear Search:
10 โ 20 โ 30 โ 40 โ 50
Binary Search:
30
/ \
10 40...
The key interview point is not simply memorizing the complexities. You should understand when Binary Search can actually be applied.
๐น 12. What is an in-place algorithm?
Answer:
An in-place algorithm modifies the input data structure while using only a small amount of additional memory.
For example, reversing an array using two pointers:
arr = [1, 2, 3, 4]
left = 0
right = len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
No second array is created.
Therefore, the extra space used is:
O(1)
๐น 13. What is the Maximum Subarray problem?
Answer:
You are given an array of integers and need to find the contiguous subarray with the maximum sum.
Example:
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
The maximum-sum contiguous subarray is:
[4, -1, 2, 1]
Its sum is:
6
A famous solution uses Kadane's Algorithm.
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
current = arr[0]
maximum = arr[0]
for i in range(1, len(arr)):
current = max(arr[i], current + arr[i])
maximum = max(maximum, current)
print(maximum)
Output:
6
โค1
Time complexity:
O(n)
This is a very important array interview problem because it tests whether you can optimize a seemingly expensive problem.
๐น 14. What is the difference between a subarray and a subsequence?
Answer:
A subarray must contain contiguous elements.
Example:
[2, 3, 4]
is a subarray of:
[1, 2, 3, 4, 5]
A subsequence does not need to be contiguous, but the original order must be maintained.
Example:
[1, 3, 5]
is a subsequence of:
[1, 2, 3, 4, 5]
but it is not a contiguous subarray.
This distinction becomes extremely important in Dynamic Programming problems such as Longest Common Subsequence.
๐น 15. How do you rotate an array?
Example:
Input:
[1, 2, 3, 4, 5]
Rotate right by 2:
[4, 5, 1, 2, 3]
One efficient approach uses the reversal technique.
Conceptually:
Reverse entire array
Reverse first k elements
Reverse remaining elements
For:
[1, 2, 3, 4, 5]
rotate right by "2":
Reverse all:
[5, 4, 3, 2, 1]
Reverse first 2:
[4, 5, 3, 2, 1]
Reverse remaining:
[4, 5, 1, 2, 3]
This technique can achieve:
Time: O(n)
Space: O(1)
Learn to recognize patterns:
โ Two Pointers
โ Sliding Window
โ Prefix Sum
โ HashMap / HashSet
โ Binary Search
โ Sorting
โ Kadane's Algorithm
โ In-place modification
Once you recognize the pattern, many seemingly different array problems become much easier to solve.
Double Tap โค๏ธ For More
O(n)
This is a very important array interview problem because it tests whether you can optimize a seemingly expensive problem.
๐น 14. What is the difference between a subarray and a subsequence?
Answer:
A subarray must contain contiguous elements.
Example:
[2, 3, 4]
is a subarray of:
[1, 2, 3, 4, 5]
A subsequence does not need to be contiguous, but the original order must be maintained.
Example:
[1, 3, 5]
is a subsequence of:
[1, 2, 3, 4, 5]
but it is not a contiguous subarray.
This distinction becomes extremely important in Dynamic Programming problems such as Longest Common Subsequence.
๐น 15. How do you rotate an array?
Example:
Input:
[1, 2, 3, 4, 5]
Rotate right by 2:
[4, 5, 1, 2, 3]
One efficient approach uses the reversal technique.
Conceptually:
Reverse entire array
Reverse first k elements
Reverse remaining elements
For:
[1, 2, 3, 4, 5]
rotate right by "2":
Reverse all:
[5, 4, 3, 2, 1]
Reverse first 2:
[4, 5, 3, 2, 1]
Reverse remaining:
[4, 5, 1, 2, 3]
This technique can achieve:
Time: O(n)
Space: O(1)
Learn to recognize patterns:
โ Two Pointers
โ Sliding Window
โ Prefix Sum
โ HashMap / HashSet
โ Binary Search
โ Sorting
โ Kadane's Algorithm
โ In-place modification
Once you recognize the pattern, many seemingly different array problems become much easier to solve.
Double Tap โค๏ธ For More
โค6
๐ฐ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐ง๐ผ ๐ ๐ฎ๐๐๐ฒ๐ฟ ๐๐ผ๐ฑ๐ถ๐ป๐ด ๐๐ผ๐ฟ ๐๐ฅ๐๐๐
โ 100% FREE YouTube Learning
โ Beginner-Friendly Programming Tutorials
โ Learn Popular & In-Demand Tech Skills
โ Study at Your Own Pace
โ Build a Strong Foundation for Coding Interviews
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐๐๐ ๐ฐ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐:-
https://pdlink.in/3WcuMvi
โก Your coding journey can start todayโcompletely FREE!
โ 100% FREE YouTube Learning
โ Beginner-Friendly Programming Tutorials
โ Learn Popular & In-Demand Tech Skills
โ Study at Your Own Pace
โ Build a Strong Foundation for Coding Interviews
๐ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐๐๐ ๐ฐ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐:-
https://pdlink.in/3WcuMvi
โก Your coding journey can start todayโcompletely FREE!