Generative AI Engineer (Part 2) — Interview 6¶
Q: How to reverse a string in Python?
- The task is to reverse the string
"hello there how are you"so that the output becomes"uoy era woh ereht olleh". - In Python, you can reverse a string using slicing with a step of
-1.
🔑 Key Steps:
- Assign the string to a variable.
- Use slicing [::-1] to reverse the string.
- Print or store the reversed string.
💻 Code:
a = 'hello there how are you' # Assign the original string to variable a
reversed_a = a[::-1] # Reverse the string using slicing with step -1
print(reversed_a) # Print the reversed string
💡 Explanation:
- a[::-1] creates a new string that is the reverse of a by starting from the end and stepping backwards.
- This is a Pythonic and efficient way to reverse any string.
- Time complexity: O(n), where n is the length of the string.
- Space complexity: O(n), as a new reversed string is created.
Q: Find the second highest number in a nested list without using any inbuilt sorting functions.
- The task is to find the second highest number from a nested list (list of lists) without using Python's built-in sorting functions.
- The optimal approach is to flatten the nested list, then iterate to find the highest and second highest values manually.
🔑 Key Steps: - Flatten the nested list into a single list of numbers. - Initialize two variables to track the highest and second highest numbers. - Iterate through the flattened list, updating the highest and second highest as needed.
💻 Code:
a = [[3, 6, 2], [9, 1, 4], [8, 7, 10]] # Nested list of numbers
# Flatten the nested list
flat_list = [] # Create an empty list to store all numbers
for sublist in a: # Iterate through each sublist in the main list
for num in sublist: # Iterate through each number in the sublist
flat_list.append(num) # Add the number to the flat_list
# Initialize variables for highest and second highest
first = second = float('-inf') # Set both to negative infinity initially
for num in flat_list: # Iterate through each number in the flat list
if num > first: # If current number is greater than first
second = first # Update second to be the old first
first = num # Update first to be the current number
elif first > num > second: # If current number is between first and second
second = num # Update second to be the current number
print("Second highest number is:", second) # Print the second highest number
💡 Explanation:
- The nested list is first flattened to make iteration easier.
- We use two variables, first and second, to keep track of the largest and second largest numbers found so far.
- As we iterate, we update these variables accordingly.
- This approach avoids using any built-in sorting and works in O(n) time, where n is the total number of elements.
- Space complexity is O(n) due to the flattened list.
Q: Rotate an array of n elements to the right by k positions.
- The task is to create a function that generates an array of n elements
[1, 2, ..., n]and rotates it to the right by k positions. - For example,
rotateArray(9, 1)should return[9, 1, 2, 3, 4, 5, 6, 7, 8].
🔑 Key Steps:
- Generate the array from 1 to n.
- Use slicing to rotate the array to the right by k positions.
- Handle cases where k > n by using k % n.
💻 Code:
def rotateArray(n, k): # Define the function with parameters n and k
arr = list(range(1, n + 1)) # Create an array from 1 to n
k = k % n # Handle cases where k > n
rotated = arr[-k:] + arr[:-k] # Rotate the array to the right by k positions
return rotated # Return the rotated array
# Example usage:
print(rotateArray(9, 1)) # Should print [9, 1, 2, 3, 4, 5, 6, 7, 8]
print(rotateArray(9, 2)) # Should print [8, 9, 1, 2, 3, 4, 5, 6, 7]
print(rotateArray(9, 3)) # Should print [7, 8, 9, 1, 2, 3, 4, 5, 6]
💡 Explanation:
- arr[-k:] gets the last k elements, and arr[:-k] gets the rest.
- Concatenating these slices rotates the array to the right by k positions.
- Using k % n ensures the rotation works even if k is greater than n.
- Time complexity: O(n), as slicing and concatenation both take linear time.
- Space complexity: O(n), as a new rotated list is created.
Q: Explain and validate the Python function to rotate an array of n elements to the right by k positions.
- The function
rotate_array(n, k)generates an array from 1 to n and rotates it right by k positions. - The code uses Python list slicing to achieve the rotation efficiently.
- The output for
rotate_array(9, 3)is[7, 8, 9, 1, 2, 3, 4, 5, 6], which is correct.
🔑 Key Steps:
- Create the array using list(range(1, n+1)).
- Use slicing: arr[-k:] gets the last k elements, arr[:-k] gets the rest.
- Concatenate these slices to form the rotated array.
- Return the rotated array.
💻 Code:
def rotate_array(n, k): # Define the function with parameters n and k
arr = list(range(1, n + 1)) # Create an array from 1 to n
rotated = arr[-k:] + arr[:-k] # Rotate the array to the right by k positions
return rotated # Return the rotated array
print(rotate_array(9, 3)) # Output: [7, 8, 9, 1, 2, 3, 4, 5, 6]
💡 Explanation:
- The function is correct and efficient, using O(n) time and space.
- Slicing is a Pythonic way to rotate arrays.
- If k is greater than n, you can use k = k % n to handle extra rotations.
- This approach is commonly used in coding interviews for array manipulation problems.
Q: What are microservices?
- Microservices are an architectural style for designing and developing software systems as a suite of small, independent, and loosely coupled services.
- Each microservice is responsible for a specific business capability and can be developed, deployed, and scaled independently.
- Microservices communicate with each other using lightweight protocols, typically HTTP/REST or messaging queues.
- They enable teams to work autonomously on different services, improving development speed, scalability, and maintainability.
- In enterprise AI and GenAI systems, microservices are crucial for modularizing components such as data ingestion, model inference, authentication, observability, and orchestration layers.
- For example, in the KGPT and MCP projects, microservices are used to separate concerns like API gateways (Apigee), authentication (OAuth2/OIDC), vector search, LLM orchestration, and monitoring, allowing for independent scaling and robust security.
- Key benefits include:
- Independent deployment and scaling of services
- Technology diversity (each service can use the most suitable tech stack)
- Fault isolation (failure in one service does not bring down the entire system)
- Easier maintenance and faster iteration cycles
- Microservices are typically managed using containerization (Docker, Kubernetes) and orchestrated with service discovery, registry, and observability tools for enterprise-grade reliability.
Q: What are the drawbacks of microservices?
- While microservices offer scalability and flexibility, they also introduce several challenges and drawbacks:
- Increased Operational Complexity: Managing many independent services requires robust orchestration, monitoring, and deployment pipelines. This can increase the operational overhead compared to monolithic architectures.
- Distributed System Challenges: Issues like network latency, partial failures, and data consistency become more prominent. Ensuring reliable inter-service communication and handling failures gracefully is complex.
- Data Management Complexity: Each microservice may have its own database, leading to challenges in maintaining data consistency, handling distributed transactions, and managing schema changes.
- Security Overhead: With multiple services communicating over the network, enforcing security (authentication, authorization, mTLS, token validation) across all endpoints is more involved. For example, in our MCP and KGPT architectures, we use OAuth2/OIDC, mTLS, and API gateways to maintain a zero-trust security posture.
- Deployment and Testing Complexity: Coordinating deployments, versioning APIs, and performing end-to-end testing across many services is more challenging than with a monolith.
- Team Expertise Requirements: Teams need expertise in distributed systems, DevOps, containerization (Docker, Kubernetes), and observability tools. As highlighted in our MCP platform risk assessment, custom microservices require critical expertise and can increase operational risk if the team is not experienced.
- Cost and Resource Overheads: Running multiple services can lead to higher infrastructure costs and resource utilization, especially if not managed efficiently.
-
Service Discovery and Observability: Implementing robust service discovery, registry, and distributed tracing is essential for troubleshooting and monitoring, as seen in our use of shared platform services for registry, identity, and observability.
-
In summary, while microservices provide modularity and scalability, they require careful planning, strong DevOps practices, and mature tooling to manage the added complexity and operational risks.
Q: How do you ensure code quality in AI/ML engineering projects?
- I follow a structured approach to maintain high code quality, especially in enterprise AI and GenAI projects:
- Static Code Analysis & Linting: We use tools like
isort,black, andrufffor code formatting and linting to enforce consistent style and catch common errors early. - Automated Testing: For infrastructure, we run static tests such as
cfn-lintfor CloudFormation templates. While some projects (like UIDS) have limited Python unit tests, we compensate with manual and pipeline-driven functional runs to validate end-to-end flows. - Dependency Management: All dependencies are tracked in requirements files, and we ensure reproducibility by pinning versions and documenting dynamic installs.
- Code Reviews: Every code change goes through peer review to catch logical errors, enforce best practices, and share knowledge across the team.
- CI/CD Integration: We use CI/CD pipelines (e.g., Azure Pipelines, Jenkins) to automate testing, linting, and deployment, ensuring that only validated code reaches production.
- Documentation & Contracts: We maintain clear input/output contracts, config models, and deployment models for each module, making it easier to validate integration points and maintain code over time.
- Known Risks & Quirks Tracking: We document known implementation quirks and risks (such as double training in
train.pyor dynamic dependency installs) to ensure transparency and facilitate debugging. - Manual & Functional Testing: For critical flows, we perform manual validation and pipeline-driven functional tests, especially when automated test coverage is limited.
-
Continuous Improvement: We regularly review our testing posture and add more automated tests and validation as the project matures.
-
This combination of automated tools, peer review, documentation, and manual validation ensures robust, maintainable, and production-ready code in all our AI engineering projects.
Q: How do you ensure code quality in AI/ML engineering projects?
- To ensure code quality, I follow a structured and multi-layered approach, especially for enterprise AI and GenAI systems:
- Configuration Management: Avoid hardcoding by using configuration files and declarative config models, ensuring flexibility and maintainability.
- Modular Design: Keep modules loosely coupled to enhance reusability and simplify maintenance.
- Code Reviews: Every code change undergoes peer review (PR process) to catch logical errors, enforce best practices, and share knowledge.
- Static Code Analysis & Linting: Use tools like
isort,black, andrufffor consistent formatting and to catch common issues early. - Automated Testing: Integrate static tests (e.g.,
cfn-lintfor infrastructure), and where possible, add functional and pipeline-driven tests. For example, in the UIDS project, we rely on CloudFormation linting and functional validation runs. - CI/CD Pipelines: Automate testing, linting, and deployment using CI/CD tools (e.g., Azure Pipelines, Jenkins) to ensure only validated code is promoted.
- Documentation & Contracts: Maintain clear input/output contracts, config models, and deployment models for each component, as outlined in our project documentation.
- Observability: Implement logging, monitoring, and observability as independent modules to track failures and performance issues efficiently.
-
Known Risks & Continuous Improvement: Document known quirks and risks (such as dynamic dependency installs or pipeline mismatches) and continuously review and improve code quality as the project evolves.
-
This comprehensive approach ensures robust, maintainable, and production-ready code, supporting both rapid iteration and long-term reliability in complex AI systems.
Q: What is agentic AI?
- Agentic AI refers to AI systems designed as autonomous agents that can perceive their environment, make decisions, and take actions to achieve specific goals, often with minimal human intervention.
- In practical terms, agentic AI architectures involve building modular, goal-driven components (agents) that can interact with each other and with external systems or tools.
- These agents can be orchestrated to perform complex workflows, such as multi-step reasoning, tool use, or dynamic decision-making, which is especially valuable in enterprise GenAI solutions.
- For example, in my recent projects (like KGPT and UIDS), we implemented agentic AI using frameworks such as LangChain, CrewAI, and Model Context Protocol (MCP). These frameworks allow LLMs to act as agents that can:
- Retrieve relevant information from vector databases (RAG)
- Call external APIs or tools
- Chain together multiple reasoning steps
- Automate data labeling, document processing, or workflow orchestration
- Agentic AI is particularly useful for:
- Automating complex business processes
- Enabling adaptive, context-aware responses in enterprise assistants
- Integrating LLMs with enterprise tools and APIs for advanced automation
- Architecturally, agentic AI systems are designed for scalability, modularity, and extensibility, often leveraging microservices, event-driven patterns, and robust configuration management for production readiness.
- In summary, agentic AI empowers AI systems to operate more autonomously, handle dynamic tasks, and deliver higher value in real-world enterprise scenarios.
Q: What are different mechanisms of data chunking?
- Data chunking is the process of splitting large documents into smaller, manageable pieces (chunks) for efficient processing, embedding, and retrieval in GenAI and RAG pipelines.
- Common mechanisms for data chunking include:
- Fixed-size Chunking: Splitting the document into chunks of a fixed number of characters, words, or bytes (e.g., every 1,000 tokens or 25 KB).
- Header-based Chunking: Using document structure (like Markdown or HTML headers) to split at logical boundaries (e.g., H1, H2, H3 headers). This preserves semantic meaning and context within each chunk.
- Recursive Chunking: If a chunk is still too large after initial splitting (e.g., after H1/H2), recursively split further at lower-level headers (H3, H4, H5) until each chunk fits the size limit.
- Semantic Chunking: Using NLP techniques to split text at sentence or paragraph boundaries, or based on semantic similarity, to ensure each chunk is contextually coherent.
- Hybrid/Adaptive Chunking: Combining header-based and size-based approaches, and merging very small chunks back together (consolidation) as long as they don’t cross higher-level semantic boundaries and stay within the size limit.
- In our Knowledge GPT pipeline, we use a hybrid approach:
- If the document is ≤ 25 KB, it is kept as a single chunk.
- For larger documents, we first split by H1/H2 headers using tools like LangChain’s MarkdownHeaderTextSplitter.
- If resulting chunks are still > 25 KB, we recursively split at H3, H4, and H5 headers.
- Very small chunks are merged (consolidated) back together, provided they don’t cross higher-level header boundaries and the combined size is ≤ 25 KB.
- This approach preserves document structure and semantic meaning, ensuring efficient downstream embedding and retrieval.
- The chunking strategy is critical for optimizing retrieval accuracy, minimizing context loss, and staying within LLM token limits during RAG operations.
Q: How do you evaluate the output of a system like "alinam"? (Assuming "alinam" refers to an LLM or RAG-based AI system)
- To evaluate the output of a generative AI or RAG system (like "alinam" or Knowledge-GPT), I use a comprehensive, multi-metric evaluation pipeline, as implemented in our KGPT Evaluation Pipeline:
- Answer Correctness: Measures if the generated answer is factually and semantically correct compared to the ground truth. This combines:
- Factual accuracy (using an LLM judge, e.g., GPT-4o)
- Semantic similarity (using embedding models)
- Reference health (e.g., checking if cited URLs are alive)
- Weighted scoring (e.g., factual accuracy 70%, semantic similarity 20%, reference health 10%)
- Answer Relevance: Assesses whether the answer actually addresses the user's question.
- Context Utilization: Evaluates how well the retrieved context chunks are used in the answer, including per-chunk and per-sentence relevance.
- Mean Reciprocal Rank (MRR): For search-based systems, measures how highly the correct answer appears in the ranked results.
- Harmful Content Detection: Checks for the presence of unsafe or inappropriate content in the generated output.
- Automated Reporting: All metrics and scores are aggregated and reported via Excel, charts, and comparison reports for release management and continuous improvement.
-
Strict Output Validation: All LLM-based evaluators enforce strict JSON output to ensure reliable parsing and downstream processing.
-
This evaluation framework ensures that the system's outputs are not only accurate and relevant but also safe, explainable, and aligned with business requirements. It supports both automated and manual review, enabling robust quality assurance for enterprise AI deployments.
Q: When should semantic search be used?
- Semantic search should be used when you need to retrieve information based on the meaning or intent behind a user's query, rather than relying on exact keyword matches.
- It is especially effective in the following scenarios:
- Natural Language Queries: When users ask questions in conversational or varied language, and you want to match conceptually similar content even if the exact words differ.
- Unstructured or Diverse Content: When your documents or knowledge base contain information that may be described in multiple ways, and you want to surface relevant results regardless of phrasing.
- Multilingual Support: For non-English queries or content, semantic search (using language-agnostic embeddings) can bridge language gaps better than lexical search.
- Enterprise Knowledge Assistants: In GenAI/RAG systems like Knowledge GPT, semantic search is crucial for retrieving contextually relevant chunks for LLMs to generate accurate answers.
- Complex or Ambiguous Queries: When queries are broad, ambiguous, or require understanding of context, semantic search can capture the underlying intent more effectively.
- In our Knowledge GPT architecture, semantic search is used:
- As the default for non-English queries.
- In hybrid modes (combined with lexical search) to maximize both precision and recall.
- For scenarios where conceptual relevance is more important than exact keyword matching, such as technical support, product information, or policy retrieval.
- Overall, semantic search enhances user experience by delivering more relevant, context-aware results, especially in AI-driven applications where understanding meaning is critical.
Q: What are containers?
- Containers are lightweight, portable, and self-sufficient units that package an application along with all its dependencies, libraries, and configuration files, ensuring consistent execution across different environments.
- Key characteristics of containers:
- Isolation: Each container runs in its own isolated environment, sharing the host OS kernel but keeping processes, file systems, and network stacks separate.
- Portability: Containers can be built once and run anywhere—on a developer’s laptop, on-premises servers, or in the cloud—without worrying about environment differences.
- Efficiency: Containers are more resource-efficient than traditional virtual machines because they don’t require a full guest OS for each instance.
- Immutability: Container images are immutable snapshots, ensuring that the application and its dependencies remain unchanged from development to production.
- In practice, containers are widely used for:
- Packaging and deploying microservices
- Running machine learning models and AI inference endpoints (e.g., deploying ML models with FastAPI in a Docker container)
- Enabling reproducible development, testing, and CI/CD pipelines
- Tools like Docker provide the platform to build, run, and manage containers, making them a foundational technology for modern DevOps, MLOps, and scalable AI deployments.
Q: How would you map requirements from an unstructured PDF to a large knowledge base of AWS services and features?
-
Summary: To map unstructured requirements from a PDF to a structured knowledge base (e.g., AWS services/features), I would build an automated pipeline leveraging document parsing, semantic chunking, embeddings, and semantic search. This enables matching each requirement to the most relevant knowledge base entries, even at scale.
-
Reasoning Process:
- The PDF is unstructured and lengthy, so we need to extract, segment, and semantically represent its content.
- The knowledge base is large (millions of records), so scalable, efficient retrieval is critical.
- Mapping should be robust to varied language and context, so semantic similarity (not just keywords) is essential.
🔑 Key Steps:
- 1. PDF Ingestion & Preprocessing
- Use a PDF parser (e.g., PyMuPDF) to extract text from the PDF.
- Convert PDF pages to HTML, then to Markdown to preserve structure (as in the Knowledge-GPT pipeline).
-
Apply a chunking strategy (e.g., header-based, recursive, or semantic chunking) to split the document into meaningful requirement chunks.
-
2. Embedding Generation
- For each chunked requirement, generate embedding vectors using a model like Azure OpenAI or similar.
-
Ensure the same embedding model is used for both requirements and knowledge base entries for consistency.
-
3. Knowledge Base Preparation
-
Ensure all knowledge base entries (AWS services, features, etc.) are already chunked and embedded, indexed in a vector database (e.g., Amazon OpenSearch, ChromaDB).
-
4. Semantic Mapping (Retrieval)
- For each requirement chunk, perform a semantic search against the knowledge base embeddings to find the most relevant AWS services/features.
-
Use top-k retrieval to get the best matches for each requirement.
-
5. Post-processing & Mapping Output
- Aggregate and rank the mappings, possibly using additional filters (e.g., confidence thresholds, domain-specific rules).
-
Optionally, use an LLM to generate a mapping report or summary, explaining which AWS services/features address each requirement.
-
6. Automation & Scalability
- Orchestrate the entire process using an ETL pipeline (e.g., AWS Glue), ensuring it can handle large volumes and resume on failure.
- Store mapping results for auditability and further analysis.
Practical Example from My Experience: - In the Knowledge-GPT pipeline, we process large volumes of enterprise documents (HTML/PDF), convert them to Markdown, chunk them semantically, generate embeddings, and bulk-ingest into OpenSearch. - For mapping, we use semantic search to match user queries or requirements to the most relevant knowledge chunks, supporting both structured and unstructured content.
- This approach ensures accurate, scalable, and explainable mapping of unstructured requirements to a large, structured knowledge base, leveraging state-of-the-art GenAI and semantic retrieval techniques.