Question 22 (Interview-Level):
Explain the difference between
Options:
A) Both modify the original list
B)
C) Shallow copy affects nested objects, deepcopy doesn't
D)
#Python #Interview #DeepCopy #MemoryManagement
✅ By: https://t.iss.one/DataScienceQ
Explain the difference between
deepcopy and regular assignment (=) in Python with a practical example. Then modify the example to show how deepcopy solves the problem. import copy
# Original Problem
original = [[1, 2], [3, 4]]
shallow_copy = original.copy()
shallow_copy[0][0] = 99
print(original) # What happens here?
# Solution with deepcopy
deep_copied = copy.deepcopy(original)
deep_copied[1][0] = 77
print(original) # What happens now?
Options:
A) Both modify the original list
B)
copy() creates fully independent copies C) Shallow copy affects nested objects, deepcopy doesn't
D)
deepcopy is slower but creates true copies #Python #Interview #DeepCopy #MemoryManagement
✅ By: https://t.iss.one/DataScienceQ
❤2
⁉️ Interview question
How does Python handle memory when processing large datasets using generators versus list comprehensions, and what are the implications for performance and garbage collection?
Simpson:
When you use a **list comprehension**, Python evaluates the entire expression immediately and stores all items in memory, which can lead to high memory usage and slower garbage collection cycles if the dataset is very large. In contrast, a **generator** produces values on-the-fly using lazy evaluation, meaning only one item is kept in memory at a time. This significantly reduces memory footprint but may slow down access if you need to iterate multiple times over the same data. Additionally, because generators don’t hold references to intermediate results, they allow earlier garbage collection of unused objects, improving overall memory efficiency. However, if you convert a generator to a list (e.g., via `list(generator)`), you lose the memory advantage. The key trade-off lies in **memory vs. speed**: lists offer faster repeated access, while generators favor memory conservation.
#️⃣ tags: #Python #AdvancedPython #DataProcessing #MemoryManagement #Generators #ListComprehension #Performance #GarbageCollection #InterviewQuestion
By: t.iss.one/DataScienceQ 🚀
How does Python handle memory when processing large datasets using generators versus list comprehensions, and what are the implications for performance and garbage collection?
Simpson:
#️⃣ tags: #Python #AdvancedPython #DataProcessing #MemoryManagement #Generators #ListComprehension #Performance #GarbageCollection #InterviewQuestion
By: t.iss.one/DataScienceQ 🚀
❔ Interview question
What is the difference between
Answer:
always creates a new copy of the input data, meaning that modifications to the original list will not affect the resulting array. This ensures data isolation but increases memory usage. In contrast, only creates a copy if the input is not already a NumPy array or compatible format—otherwise, it returns a view of the existing data. This makes asarray() more memory-efficient when working with existing arrays or array-like objects. For example, if you pass an existing NumPy array to asarray(), it returns the same object without copying, whereas array() would still create a new copy even if the input is already a NumPy array
tags: #Python #NumPy #MemoryManagement #DataConversion #ArrayOperations #InterviewQuestion
By: @DataScienceQ 🚀
What is the difference between
numpy.array() and numpy.asarray() when converting a Python list to a NumPy array, and how does it affect memory usage?Answer:
numpy.array()numpy.asarray()tags: #Python #NumPy #MemoryManagement #DataConversion #ArrayOperations #InterviewQuestion
By: @DataScienceQ 🚀
❤4
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p;
p.x = 10;
p.y = 20;
std::cout << "Point: (" << p.x << ", " << p.y << ")";
return 0;
}
Point: (10, 20)
#43.
publicAn access specifier that makes class members accessible from outside the class.
#include <iostream>
class MyClass {
public: // Accessible from anywhere
int myNum = 10;
};
int main() {
MyClass obj;
std::cout << obj.myNum;
return 0;
}
10
#44.
privateAn access specifier that makes class members accessible only from within the class itself.
#include <iostream>
class MyClass {
private:
int secret = 42;
public:
void printSecret() {
std::cout << secret; // Accessible from within the class
}
};
int main() {
MyClass obj;
// std::cout << obj.secret; // This would cause a compile error
obj.printSecret();
return 0;
}
42
#45. Constructor
A special member function of a class that is executed whenever a new object of that class is created.
#include <iostream>
class Car {
public:
// Constructor
Car() {
std::cout << "Car object created.";
}
};
int main() {
Car myCar; // Constructor is called here
return 0;
}
Car object created.
---
#CPP #OOP #MemoryManagement
#46. Destructor
A special member function that is executed automatically when an object is destroyed.
#include <iostream>
class MyClass {
public:
// Destructor
~MyClass() {
std::cout << "Object destroyed.";
}
};
int main() {
MyClass obj;
// Destructor is called when main() ends
return 0;
}
Object destroyed.
#47.
thisA keyword that refers to the current instance of the class.
#include <iostream>
class Box {
private:
int length;
public:
Box(int length) {
this->length = length; // Use 'this' to distinguish member from parameter
}
void printLength() {
std::cout << "Length: " << this->length;
}
};
int main() {
Box b(10);
b.printLength();
return 0;
}
Length: 10
#48.
newAn operator that allocates memory on the heap and returns a pointer to it.
#include <iostream>
int main() {
int* ptr = new int; // Allocate an integer on the heap
*ptr = 100;
std::cout << "Value from heap: " << *ptr;
delete ptr; // Must deallocate memory
return 0;
}
Value from heap: 100
#49.
deleteAn operator that deallocates memory previously allocated with
new.#include <iostream>
int main() {
int* ptr = new int(55);
std::cout << *ptr << " allocated. ";
delete ptr; // Deallocate the memory
std::cout << "Memory freed.";
// Accessing ptr now is undefined behavior
return 0;
}
55 allocated. Memory freed.
#50.
nullptrRepresents a null pointer literal. It indicates that a pointer does not point to any valid memory location.
#include <iostream>
int main() {
int* ptr = nullptr;
if (ptr == nullptr) {
std::cout << "The pointer is null.";
}
return 0;
}
The pointer is null.
━━━━━━━━━━━━━━━
By: @DataScienceQ ✨
❤1