How would you implement a dequeue (double-ended queue) data structure?
- Array
- Linked List
- Queue
- Stack
A dequeue can be efficiently implemented using a doubly linked list, where you can add or remove elements from both ends in constant time. While arrays are also used, they may not provide the same level of efficiency for both ends' operations.
How would you implement a stack in Python?
- Using a dictionary
- Using a list
- Using a set
- Using a tuple
In Python, you can implement a stack using a list. Lists provide built-in methods like append() and pop() that make it easy to simulate a stack's behavior.
How would you implement rate limiting in a RESTful API to prevent abuse?
- A. Use JWT tokens
- B. Implement a token bucket algorithm
- C. Limit requests based on IP addresses
- D. Disable API access during peak hours
B. Implementing a token bucket algorithm is a common method for rate limiting in RESTful APIs. It allows you to control the rate of requests over time, preventing abuse while allowing legitimate usage. Options A, C, and D are not effective methods for rate limiting.
How would you initialize an empty list in Python?
- empty_list = []
- empty_list = [None]
- empty_list = {}
- empty_list = None
To initialize an empty list in Python, you use square brackets []. Option 2 initializes an empty dictionary, option 3 initializes a variable as None, and option 4 initializes a list with a single element, which is None.
How would you investigate memory leaks in a Python application?
- Manually inspect each variable in the code to find memory leaks.
- Use a memory profiler like memory_profiler to track memory usage over time.
- Use the psutil library to monitor CPU usage and infer memory leaks.
- Use the timeit module to measure execution time and find memory leaks.
To investigate memory leaks in a Python application, you can use a memory profiler like memory_profiler, which tracks memory usage over time, helping you identify areas of concern. Manual inspection (Option 3) is impractical for large codebases, and psutil (Option 2) primarily focuses on CPU usage. The timeit module (Option 4) measures execution time, not memory usage.
In Pandas, how can you filter the rows of a DataFrame where the value in a specific column is greater than a threshold?
- df.apply('column_name > threshold')
- df.filter('column_name > threshold')
- df.select('column_name').where('value > threshold')
- df[df['column_name'] > threshold]
To filter rows in a Pandas DataFrame where the value in a specific column is greater than a threshold, you can use boolean indexing. This is achieved by specifying the condition inside square brackets.
In Pandas, how do you access the first five rows of a DataFrame?
- df.head()
- df.iloc[:5]
- df.loc[:5]
- df[0:5]
To access the first five rows of a Pandas DataFrame, you should use the head() method, like df.head(). This method returns the top N rows (default is 5) of the DataFrame.
In pytest, the ____ fixture is used to execute specific finalization code after the test function has completed.
- cleanup
- finalize
- teardown
- yield_fixture
In pytest, the teardown fixture is used to execute finalization code after the test function has completed its execution. This allows you to perform cleanup tasks or teardown actions after the test has run, such as closing a database connection or cleaning up temporary files.
In pytest, the ____ fixture is used to pass command-line options to test functions.
- @pytest.cmdline
- @pytest.config
- @pytest.fixture(params)
- @pytest.options
In pytest, the @pytest.config fixture is used to pass command-line options and configuration values to test functions. This allows you to customize the behavior of your tests based on configuration settings.
In pytest, the ____ marker is used to skip a test function under certain conditions.
- @pytest.ignore
- @pytest.run
- @pytest.skip
- @pytest.xfail
In pytest, the @pytest.skip marker is used to skip a test function when certain conditions are met. This allows you to selectively skip tests based on runtime conditions or configurations.