Continue on the technical part of interview. I am a full stack developer and here I answered some questions about network, REST, Spring and React frameworks.
What is HTTP? How does it work?
HTTP is a set of instructions for applications/clients/web servers to communicate to each other over a web, when client requests a specific resource and gets a response message. Request and response comes in defined structure. They consist of three parts – start line, header and body. HTTP is:
- TCP/IP based protocol
- stateless – client and server just know about each other just during the current request
- connectionless – after a client request is made, the client disconnects from the server and waits for response. When server
- prepares a response it re-establishes the connection to send a response back
- can transfer hypermedia – any type of data like video, documents, text, etc.
HTTP Status code families
- 2xx – success
- 3xx – redirection
- 4xx – client errors
- 5xx – server errors
Main HTTP methods
- GET – retrieves whatever information (in the form of an entity) is identified by the Request-URI
- POST – provides a block of data, such as the result of submitting a form, to a data-handling process, which creates/updates the resource on the server
- PUT – is pretty much as POST. It is used to create/replace a resource. The main difference is that PUT is idempotent – for the same request always returns the same response
- DELETE – delete resource defined by URI
What is TCP/IP?
TCP/IP is a protocol or a set of rules, which specifies communication over the internet. It tell how data should be packetized, addressed, transmitted, routed, and received. TCP/IP consists of 4 layers:
- application – is the actual running program which wants to send data. Application uses HTTP, SMTP etc.
- transport layer is where TCP sits and it talks to application level through ports. TCP chops data in small chunks so that they can be sent to the receiver over the internet
- internet is where IP sits. It attaches origin and destination IP addresses
- network – manages communication with actual physical machine and converts data to/from electrical impulses
TCP vs UDP?
Both are protocols for sending and receiving data over the network. Data is chopped into small chunks and sent from sender to receiver. The main difference is that TCP is connection oriented protocol, when UDP is connectionless one. Connection oriented means that data delivery is guaranteed. If any packet is missing – it is redelivered. Also TCP provides order for packets. UDP is also known as “fire and forget” protocol. That means, when sender just sends data and doesn’t care about whether receiver received them or not. UDP is much faster than TCP. TCP is used for websites, file transfers; UDP is popular in broadcast, gaming, etc.
What are the key differences between HTTP/1.x and HTTP2?
At a high level, HTTP/2:
- is binary, instead of textual. Binary protocols are more efficient to parse, more compact “on the wire”, and most importantly, they are much less error-prone, compared to textual protocols like HTTP/1.x, because they often have a number of affordances to “help” with things like whitespace handling, capitalization, line endings, blank lines and so on.
is fully multiplexed, instead of ordered and blocking. HTTP/1.x has a problem called “head-of-line blocking,” where effectively only one request can be outstanding on a connection at a time
- can therefore use one connection for parallelism. With HTTP/1, browsers open between four and eight connections per origin(more than 30…). One application opening so many connections simultaneously breaks a lot of the assumptions that TCP was built upon; since each connection will start a flood of data in the response, there’s a real risk that buffers in the intervening network will overflow, causing a congestion event and retransmits
- allows servers to “push” responses proactively into client caches. Normally browser sends a request to a server to get a web page. Server sends a response with HTML until browser finishes parsing HTML and issue requests for all embedded assets. Server Push potentially allows the server to avoid this round trip of delay by “pushing” the responses it thinks the client will need into its cache. However, Pushing responses is not “magical” – if used incorrectly, it can harm performance
- uses header compression to reduce overhead. If you assume that a page has about 80 assets (which is conservative in today’s Web), and each request has 1400 bytes of headers (again, not uncommon, thanks to Cookies, Referer, etc.), it takes at least 7-8 round trips to get the headers out “on the wire.”. In comparison, even mild compression on headers allows those requests to get onto the wire within one roundtrip
What is REST?
Representational State Transfer is an architectural style of developing web services. A resource can be data or action identified by URI via HTTP and represented by JSON, XML or plain text (JSON strongly recommended). REST provides communication between server and a client. Rest is stateless so server and a client can understand messages without knowing anything about previous messages.
How to introduce a change for API and do not break the current contracts?
- backward compatibility
What are commonly used HTTP methods supported by REST?
- GET – read
- POST – create. The only method which can have payload
- PUT – update/replace
- PATCH – update/modify
- DELETE – delete
What is HTTP basic authentication and how does it work?
HTTP Basic authentication (BA) implementation is the simplest technique for enforcing access controls to web resources because it does not require cookies, session identifiers, or login pages; rather, HTTP Basic authentication uses standard fields in the HTTP header. Client enters username and password, then they are encoded by Base64, added to “Authorization” header and sent to the server.
What are advantages of REST web services?
- communication through HTTP and no coupling on underlying technology
- supports multiple data formats
- lightweight protocol
- testable from browser
What are disadvantages of REST web services?
- since there is no contract defined between service and client, it has to be communicated through other means such as documentation or emails
- since it works on HTTP, there can’t be asynchronous calls
- sessions can’t be maintained
- componentWillMount – no component yet so you can’t do anything involving DOM. Most use case: app configuration in your root component. Don’t call setState, use default values here instead
- componentDidMount – add listeners, draw on <canvas/>. Most use case: AJAX calls. Call setState here
- componentWillReceiveProps – parent component changed the props. Most common use case: acting on particular props change to trigger state transition. Call setState here
- shouldComponentUpdate – after props change, it asks a question “should I render it?” with Boolean answer. Most common use case: controlling whether a component to re-render. Especially when performance is important. Don’t call setState here
- componentWillUpdate – functionally it is the same as componentWillReceiveProps, except you are not allowed to call setState
- componentDidUpdate – similar to componentDidMount. Most common use case: updating DOM in order to prop or state changes
- componentWillUnmount – cancel network requests, remove listeners. Most common use case: cleanup leftovers. Don’t call setState here
What are the differences between state and props?
- the state of one component will often become the props of a child component. A Component cannot change its props, but it is responsible for putting together the props of its child Components. Props do not have to just be data — callback functions may be passed in as props
- the state is a data structure that starts with a default value when a Component mounts. It may be mutated across time, mostly as a result of user events
- component manages its own state internally. You might conceptualize state as private to that component
Why is it wrong to use document.getElementById in React?
What is one thing you don’t like in React?
I guess this must be personal for everyone. But for example I don’t like boilerplate code in ES6 when I need to repeat .bind(this) all the time.
What is Spring?
Spring is an open source development framework for Enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make Java EE development easier to use and promote good programming practice by enabling a POJO-based programming model.
What are features of Spring?
- lightweight – Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB and it’s easy to add additional modules
- inversion of control (IOC) – Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects
- aspect oriented (AOP) – Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services
- container – Spring contains and manages the life cycle and configuration of application objects
- MVC framework – Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.
transaction Management – Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring’s transaction support is not tied to J2EE environments and it can be also used in container less environments.
List some Spring framework modules
- Core module
- AOM module
- JDBC module
- Java Messaging Service(JMS) module
- Transaction module
- Web module
What is IoC?
Inversion of Control is a common pattern in the Java community that helps wire lightweight containers or assemble components from different projects into a cohesive application. Object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.
The Spring IoC container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction.
What are the types of Dependency Injection Spring supports?
- setter Injection – Setter-based DI is realized by calling setter methods on the user’s beans after invoking a no-argument constructor or no-argument static factory method to instantiate their bean
- constructor Injection – Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator
What are Spring beans?
The objects that form the backbone of the users application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that the users supply to the container.
What bean scopes does Spring support? Explain them.
The Spring Framework supports following five scopes, three of which are available only if the users use a web-aware Application Context.
- singleton(Default): This scopes the bean definition to a single instance per Spring IoC container
- prototype: This scopes a single bean definition to have any number of object instances.
- request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext
- session: This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext
- global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
Explain Bean lifecycle in Spring framework?
- instantiate: First the spring container finds the bean’s definition from the XML file or @Configuration or scanned packages and instantiates the bean
- populate properties: Using the dependency injection, spring populates all of the properties as specified in the bean definition
- set bean name: If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method
set bean factory: If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method
- pre initialization: Also called post process of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method
- initialize bean: If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called
- post initialization: – If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called
- ready to use: Now the bean is ready to use by the application
- destroy: If the bean implements DisposableBean, it will call the destroy() method
What are Spring Boot features?
- application server
- Java configuration + annotation based configuration
- actuator – health check(AbstractHealthIndicator)
- easy dependency management -> starters
- YAML support
Transactions in Spring
- provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.
Supports declarative transaction management
- provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA.
Integrates very well with Spring’s various data access abstractions
Interviewing part 2.3: OOP, programming languages and philosophy <– previous post