Start by defining both concepts clearly, then contrast them with a simple example. Emphasize the trade-offs: inheritance for 'is-a' relationships and composition for 'has-a' relationships, and explain why composition is often preferred for flexibility and maintainability.
Pro tip: Mention the 'favor composition over inheritance' principle from Design Patterns, and give a real-world example where inheritance caused a fragile hierarchy, showing you understand practical implications.
Explain that inheritance is a mechanism where a class derives from another, inheriting its properties and methods, representing an 'is-a' relationship.
Explain that composition involves building classes by combining simpler objects, representing a 'has-a' relationship, where the composed object delegates behavior.
Provide a concrete example, such as a Car class inheriting from Vehicle (inheritance) versus a Car class having an Engine (composition), to illustrate the difference.
Highlight pros and cons: inheritance promotes code reuse but creates tight coupling; composition offers flexibility, easier testing, and avoids fragile base class problems.
State that composition is generally preferred for flexibility, but inheritance is useful for true 'is-a' relationships with shared behavior, referencing the 'favor composition over inheritance' principle.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
First, read the code carefully and explain its intended behavior in plain English, then trace through the execution step by step to identify any runtime errors or logical bugs. Finally, propose specific fixes and verify them by mentally running the corrected code or describing expected output.
Pro tip: Always mention edge cases and potential exceptions (e.g., NullPointerException, ArrayIndexOutOfBoundsException) even if not explicitly asked—it shows thoroughness and production-ready thinking. Also, briefly discuss time/space complexity if relevant, as Walmart values efficient solutions.
Read the code and summarize what it is supposed to do in simple terms, identifying inputs, outputs, and core logic.
Walk through the code line by line with sample inputs to spot compile-time errors, runtime exceptions, or logical flaws.
For each error, suggest a concrete fix and explain why it resolves the issue without breaking intended behavior.
Mentally run the fixed code with normal and edge-case inputs to confirm correctness, and mention any remaining assumptions or limitations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Eight years touching Java, professionally and as hobby projects, and I just...
Start with a clear, concise definition of a Stream as a sequence of elements supporting functional-style operations, then explain its purpose for declarative data processing. Walk through a practical example (e.g., filtering and collecting) to illustrate lazy evaluation and pipeline structure, and highlight how it differs from collections.
Pro tip: Emphasize that streams are lazy and can be parallelized, but caution that parallel streams aren't always faster—showing you understand trade-offs, not just syntax.
State that a Stream is a sequence of elements from a source that supports aggregate operations, introduced in Java 8, and is not a data structure itself.
Describe the three parts: source, intermediate operations (e.g., filter, map), and terminal operation (e.g., collect, forEach), noting that intermediate operations are lazy.
Provide a short code snippet, such as filtering a list of strings and collecting results, to demonstrate usage and readability.
Mention declarative style, potential for parallelism, and improved code clarity, but also note limitations like single-use and overhead.
Briefly explain that collections store data, while streams process it, and that streams are consumed once.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.