Python Data Science Jobs & Interviews
20.6K subscribers
192 photos
4 videos
25 files
334 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
Interview question

What does the following code do?
import os
os.makedirs("folder/subfolder", exist_ok=True)

Answer:
Creates a directory named 'folder' with a subdirectory 'subfolder' if it doesn't already exist

tags: #python #os #filehandling #coding #programming #directory #makedirs #dev

By: t.iss.one/DataScienceQ 🚀
1
⁉️ Interview question
How does Python’s mmap module behave when mapping a file that is concurrently being truncated by another process using os.ftruncate()? What are the implications for memory safety, and under what conditions might this lead to segmentation faults or undefined behavior?

When a file is mapped via `mmap` and simultaneously truncated by another process, the virtual memory pages remain valid until accessed. However, if the mapped region refers to data beyond the new file size, accessing those pages results in undefined behavior, potentially causing segmentation faults. The operating system may not immediately invalidate the mappings, leading to crashes or data corruption. This scenario highlights the need for synchronization mechanisms like file locks or signals to ensure safe concurrent access

#️⃣ tags: #Python #AdvancedPython #FileHandling #MemoryMapping #mmap #ConcurrentProgramming #OS #SystemCalls #UndefinedBehavior #SegmentationFault #FileLocking

By: t.iss.one/DataScienceQ 🚀
Please open Telegram to view this post
VIEW IN TELEGRAM
⁉️ Interview question
What happens when you use os.fdopen() to wrap a file descriptor that was opened with O_DIRECT flag on a Linux system, and then attempt to read or write using Python’s buffered I/O? How does this affect data consistency and performance?

When a file descriptor opened with `O_DIRECT` is wrapped by `os.fdopen()`, Python’s buffered I/O may interfere with the direct I/O semantics because it uses its own internal buffer. This can lead to data being copied through the kernel’s page cache, effectively bypassing the `O_DIRECT` requirement for direct memory-to-disk transfers. As a result, performance gains from `O_DIRECT` are lost, and data consistency may be compromised if the buffer isn’t flushed properly. Additionally, misaligned memory access due to Python’s buffering can cause crashes or undefined behavior.

#️⃣ tags: #Python #AdvancedPython #FileHandling #OS #Linux #O_DIRECT #BufferedIO #SystemCalls #Performance #DataConsistency #LowLevelProgramming

By: t.iss.one/DataScienceQ 🚀
⁉️ Interview question
What happens when you use os.link() to create a hard link to a file that is already open in write mode by another process, and how does this affect the file’s inode reference count, data integrity, and potential for race conditions during deletion?

Creating a hard link via `os.link()` increases the inode reference count, meaning the file won’t be deleted until all links are removed. However, if the original file is being written to, the new link points to the same underlying data blocks. If the original file is truncated or deleted while the link exists, the data remains accessible through the link until all processes close it. This can lead to data inconsistency if the writing process modifies the file size but the link still references old data. Additionally, concurrent operations on the same inode without proper synchronization may cause corruption or unexpected behavior.

#️⃣ tags: #Python #AdvancedPython #FileHandling #HardLink #Inode #OS #RaceCondition #DataIntegrity #FileOperations #SystemCalls #Linux #FileDeletion

By: t.iss.one/DataScienceQ 🚀
Interview question

Why is it better to use os.path.join() to construct paths instead of simple string concatenation?

Answer: Because os.path.join() handles cross-platform compatibility automatically. Operating systems use different path separators (e.g., / for Linux/macOS and \ for Windows). Hardcoding a separator like 'folder' + '/' + 'file' will break on a different OS. os.path.join('folder', 'file') correctly produces folder/file or folder\file depending on the system, making the code robust and portable.

tags: #interview #python #os

━━━━━━━━━━━━━━━
By: @DataScienceQ
1