Contractor interview real Q

. . .

Frontend

  • Redux,
  • React Router,
  • SPAs,
  • Material UI,
  • Jest and React Testing Library,
  • responsive design and adaptive design
  • html5 new features
  • event delegation in js
  • how do you manage state in react application?
  • react hooks? why use it
  • virtual dom
  • webpack, why use it
  • jsx

amazon contractor

JavaScript Questions

  1. What are the differences between let, const, and var?
    let and const are block-scoped; const can’t be reassigned. var is function-scoped and hoisted, which may cause bugs in closures.

    • example1:

      const a = [1,2,3];
      const b = a;
      b[0] = 'dean';
      console.log(a, b);
      // what is printed
    • or

      console.log(a)
      a = 2;
      // what is printed
    • or

      console.log(a)
      var a = 1;
      // what is printed

      console.log(b)
      let b = 2;
      // what is printed
  2. What is a Promise in JavaScript?
    A Promise represents the result of an asynchronous operation. It can be pending, fulfilled, or rejected, and is used with .then() or await.

  3. What is a Closure in JavaScript?
    A closure is a function that remembers variables from its lexical scope, even when executed outside of that scope. It enables private data and function factories.

  4. What is the Event Loop?
    The event loop allows non-blocking async execution. It processes the call stack and executes callbacks from the message or microtask queues.

  5. What does typeof typeof 1 return?
    It returns “string” because typeof 1 is “number”, and typeof “number” is “string”.

  6. What is the output of 1 < 2 < 3 and 3 > 2 > 1?
    1 < 2 < 3 is true, but 3 > 2 > 1 is false because it evaluates to true > 1 → 1 > 1 → false.

  7. What is the difference between undefined and null?
    undefined means a variable was declared but not assigned. null is an intentional absence of value set by the programmer.

  8. What is the unknown type in TypeScript?
    unknown is a safer alternative to any. You must check or assert the type before using it, ensuring type safety.

  9. What is the difference between call and apply?
    call() and apply() both invoke functions with a specified this context. call() passes arguments individually, while apply() expects an array of arguments.

how to center a div on a screen

css position problem, difference between fixed, absolutely and relative

event propagation or event delegation

bubbling phase

capture phase

original question is: “how can l add capture phase instead of it by default? it will be like bubbling even should be handling the bubbling face, right? How can l make it? To capture phase, like, What parameter should l? Like do you know how to make? Add even listener to the capture phase.”

React Questions

  1. What is Virtual DOM in React?
    The Virtual DOM is an in-memory representation of the real DOM. React updates it first, diffs it, and only patches the changed nodes to the actual DOM.

  2. What are React Hooks (useState, useEffect, useMemo)?
    Hooks let function components use state and lifecycle logic. useState manages local state, useEffect handles side effects, and useMemo caches computed values.

  3. What is useEffect used for?
    useEffect runs after render and handles side effects like API calls, subscriptions, and cleanup logic. It runs on mount, update, and unmount depending on the dependency array.

  4. What is a controlled vs uncontrolled component?
    Controlled components store input value in state, updated via onChange. Uncontrolled ones use refs to access input value directly.

  5. What is prop drilling? How do you avoid it?
    Prop drilling is passing props through multiple intermediate components. It can be avoided using React Context API or global state libraries like Redux.

  6. What is the difference between props and state?
    Props are external, read-only data passed from parent to child. State is internal and mutable, managed within the component.

  7. What is the React component structure and data flow?
    React uses a top-down (unidirectional) data flow. Parent components pass data via props, and child components can lift state updates via callbacks.

  8. What is an Error Boundary?
    An Error Boundary is a class component that catches rendering errors in its child tree. It renders fallback UI instead of crashing the entire app.

  9. What is a Higher Order Component (HOC)?
    A Higher Order Component is a function that takes a component and returns a new component with additional features. It’s used for logic reuse like authentication or data fetching.

  10. Describe the React component lifecycle.
    Functional components use useEffect to simulate lifecycle phases. It runs after mount, on update (with dependencies), and supports cleanup on unmount.

  11. How does Redux work? What are reducer/store/dispatch?
    Redux uses a global store to hold state. dispatch() sends actions, and reducers are pure functions that return the updated state based on those actions.

  12. Difference between Redux and Context API?
    Context API is built into React and is ideal for simple global data like theme or auth. Redux is better for large-scale apps, with middleware, devtools, and structured state management.

Spring Boot

In java, difference between @Controller and @RestController

Spring Boot Notes

In Java, specifically in the Spring framework, both @Controller and @RestController are annotations used for
building web - related components. Here are the key differences between them:

1. Function and Purpose

  • @Controller
    • It is a stereotype annotation in Spring MVC. It is mainly used to mark a class as a Spring MVC controller. A
      controller class typically handles HTTP requests, processes them, and then returns a view name. The view resolver
      in Spring MVC will then use this view name to render an appropriate view (such as a JSP page, Thymeleaf template,
      etc.) to the client.
  • @RestController
    • It is a combination of @Controller and @ResponseBody. This annotation is designed for building RESTful web
      services. When a class is marked with @RestController, all the handler methods in this class will automatically
      serialize the return value to the HTTP response body in a format like JSON or XML by default.

2. Return Value Handling

  • @Controller
    • By default, the return value of a method in a @Controller class is treated as a view name. For example:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MyViewController {
@GetMapping("/view")
public String showView() {
return "myView";
}
}
- In this code, the method `showView` returns the string `"myView"`, and Spring MVC will look for a view with this name to render.
  • @RestController
    • The return value of a method in a @RestController class is directly serialized and sent as the HTTP response
      body. For example:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {
@GetMapping("/data")
public String getData() {
return "This is some data";
}
}
- When a client makes a request to `/data`, the string `"This is some data"` will be sent directly in the response body.

3. Usage Scenarios

  • @Controller
    • It is suitable for traditional web applications where you need to render views based on user requests. For
      example, an e - commerce website where you need to display product lists, user profiles, etc. in HTML pages.
  • @RestController
    • It is ideal for building RESTful APIs. These APIs can be consumed by various clients such as mobile applications,
      single - page web applications, etc. For example, a backend API for a mobile banking application that provides
      services like getting account balance, transaction history, etc.

Kafka

in kafka, how to handle schema evolution

MQ notes

Here are several common methods to handle schema evolution in Apache Kafka:

1. Using Schema Registry

Avro with Confluent Schema Registry

  • Principle
    • Confluent Schema Registry is a widely used tool for managing schemas in Kafka. It stores schemas in a central
      repository and enforces compatibility rules between different versions of schemas. When using Avro serialization,
      producers and consumers can interact with the Schema Registry to ensure that the data they produce and consume
      adheres to the defined schemas.
  • Steps
    • Producer side: When a producer sends data, it first checks if the schema of the data exists in the Schema
      Registry. If not, it registers the new schema. The registry then assigns a unique ID to the schema. The producer
      serializes the data using Avro and includes the schema ID in the message.
    • Consumer side: The consumer receives the message with the schema ID, fetches the corresponding schema from the
      Schema Registry, and deserializes the data using that schema.
  • Compatibility Modes
    • Backward compatibility: Newer versions of the schema can be read by older consumers. For example, adding a new
      optional field to an Avro record is backward - compatible.
    • Forward compatibility: Older versions of the schema can be read by newer consumers. For example, removing a
      non - required field is forward - compatible.
    • Full compatibility: It combines backward and forward compatibility.

Protobuf with Schema Registry

  • Similar to Avro, Protocol Buffers can also be used with a schema registry. Protobuf has its own serialization and
    deserialization mechanisms, and the schema registry helps in managing different versions of Protobuf schemas.

2. Designing Flexible Schemas

Use Optional Fields

  • When defining your data schema, mark non - essential fields as optional. For example, in Avro, you can define a field
    like this:
{
"name": "optionalField",
"type": [
"null",
"string"
],
"default": null
}
  • This allows you to add new fields in future schema versions without breaking existing consumers that may not expect
    these new fields.

Avoid Breaking Changes

  • Do not remove or rename existing required fields. If you need to change the meaning of a field, it’s better to add a
    new field and gradually deprecate the old one.

3. Versioning in the Application Code

Producer - Side Versioning

  • The producer can explicitly include a version number in the message. For example, you can add a version field to
    your data structure:
public class MyMessage {
private int version;
private String data;

// Getters and setters
}
  • The producer can increment the version number when there is a schema change.

Consumer - Side Version Handling

  • The consumer can check the version number in the message and handle the data differently based on the version. For
    example:
if(message.getVersion() ==1){
// Handle data in version 1 format
}else if(message.

getVersion() ==2){
// Handle data in version 2 format
}

4. Using Kafka Connect with Schema Evolution Support

  • Kafka Connect is a tool for moving data between Kafka and other systems. Some connectors support schema evolution. For
    example, the JDBC connector can handle changes in the database schema and propagate these changes to Kafka topics
    while maintaining compatibility.

在 Apache Kafka 中,处理模式演变(Schema Evolution)有以下几种常见方法:

1. 使用模式注册表

结合 Confluent 模式注册表使用 Avro

  • 原理
    • Confluent 模式注册表是 Kafka 中广泛使用的管理模式的工具。它将模式存储在中央存储库中,并对不同版本的模式执行兼容性规则。在使用
      Avro 序列化时,生产者和消费者可以与模式注册表交互,以确保它们生产和消费的数据符合定义的模式。
  • 步骤
    • 生产者端:生产者发送数据时,首先检查数据的模式是否存在于模式注册表中。如果不存在,则注册新模式。注册表会为该模式分配一个唯一的
      ID。生产者使用 Avro 对数据进行序列化,并在消息中包含模式 ID。
    • 消费者端:消费者接收到包含模式 ID 的消息后,从模式注册表中获取相应的模式,并使用该模式对数据进行反序列化。
  • 兼容性模式
    • 向后兼容:新模式可以被旧版本的消费者读取。例如,向 Avro 记录中添加一个新的可选字段就是向后兼容的。
    • 向前兼容:旧模式可以被新版本的消费者读取。例如,移除一个非必需字段是向前兼容的。
    • 完全兼容:同时具备向后兼容和向前兼容的特性。

结合模式注册表使用 Protocol Buffers(Protobuf)

  • 与 Avro 类似,Protocol Buffers 也可以与模式注册表结合使用。Protobuf 有自己的序列化和反序列化机制,模式注册表有助于管理不同版本的
    Protobuf 模式。

2. 设计灵活的模式

使用可选字段

  • 在定义数据模式时,将非必需字段标记为可选。例如,在 Avro 中可以这样定义一个字段:
{
"name": "optionalField",
"type": [
"null",
"string"
],
"default": null
}
  • 这样,在未来的模式版本中添加新字段时,不会破坏可能不期望这些新字段的现有消费者。

避免重大更改

  • 不要移除或重命名现有的必需字段。如果需要更改字段的含义,最好添加一个新字段,并逐步弃用旧字段。

3. 在应用代码中进行版本控制

生产者端版本控制

  • 生产者可以在消息中显式包含版本号。例如,可以在数据结构中添加一个 version 字段:
public class MyMessage {
private int version;
private String data;

// Getters 和 setters
}
  • 当模式发生变化时,生产者可以递增版本号。

消费者端版本处理

  • 消费者可以检查消息中的版本号,并根据版本不同对数据进行不同的处理。例如:
if(message.getVersion() ==1){
// 处理版本 1 格式的数据
}else if(message.

getVersion() ==2){
// 处理版本 2 格式的数据
}

4. 使用支持模式演变的 Kafka Connect

  • Kafka Connect 是用于在 Kafka 和其他系统之间移动数据的工具。一些连接器支持模式演变。例如,JDBC 连接器可以处理数据库模式的更改,并将这些更改传播到
    Kafka 主题,同时保持兼容性。

kafka Exactly one semantics

MQ notes

RabbitMQ

what’s a binding in RabbitMQ?the diff between binding exchange and queue?

see the post: MQ notes
MQ notes

AWS

how to introduced S3 to your spring boot project?

chatgpt: how to introduced S3 to your spring boot project? 用简短的口语回答

I introduced S3 by adding the AWS SDK to the project, set up the credentials using IAM, then used the AmazonS3 client to upload and download files. I also configured a service class to abstract S3 operations and handled things like bucket names and regions with application properties. I tested everything locally first with a test bucket. Then I added exception handling for common issues like missing files or invalid credentials. Later on, I also added support for generating pre-signed URLs so clients could upload directly. Finally, I secured the access by using IAM roles instead of hardcoding credentials.

how to control the access of the object stored in S3?

To control access to S3 objects, I mainly use IAM policies to define who can access which buckets or objects. For public files, I set the object’s ACL to public-read. For private access, I rely on IAM roles or policies attached to EC2 or Lambda. I also use pre-signed URLs to grant temporary access to specific users without exposing credentials. Additionally, I configure bucket policies to allow or deny access based on IP, user, or specific actions.

Memory

memory improvement

Here are some common methods to improve memory usage in Java:

1. Optimize Object Creation

  • Reuse Objects: Instead of creating new objects repeatedly, reuse existing ones when possible. For example, in a loop where you need to format dates, reuse a SimpleDateFormat object instead of creating a new one each time.

    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class DateFormatExample {
    public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    for (int i = 0; i < 10; i++) {
    String formattedDate = sdf.format(new Date());
    System.out.println(formattedDate);
    }
    }
    }
  • Use Object Pools: For objects that are expensive to create, such as database connections or threads, use object pools. Libraries like Apache Commons Pool can help you implement object pools easily.

2. Manage Memory Efficiently in Collections

  • Choose the Right Collection: Select the appropriate collection type based on your usage scenario. For example, if you need a collection with fast look - up, use HashMap; if you need a sorted collection, use TreeMap.

    import java.util.HashMap;
    import java.util.Map;

    public class MapExample {
    public static void main(String[] args) {
    Map<String, Integer> map = new HashMap<>();
    map.put("one", 1);
    map.put("two", 2);
    System.out.println(map.get("one"));
    }
    }
  • Trim Collections: If you are using collections like ArrayList or HashMap, and you know that the size will not increase significantly after a certain point, you can trim the excess capacity. For ArrayList, you can use the trimToSize() method.

3. Avoid Memory Leaks

  • Properly Close Resources: Make sure to close resources such as files, database connections, and network sockets properly. You can use the try - with - resources statement introduced in Java 7 to ensure automatic resource management.

    import java.io.FileInputStream;
    import java.io.IOException;

    public class ResourceExample {
    public static void main(String[] args) {
    try (FileInputStream fis = new FileInputStream("test.txt")) {
    // Use the file input stream
    int data;
    while ((data = fis.read()) != -1) {
    System.out.print((char) data);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
  • Weak References: Use weak references (WeakReference) when you need to hold a reference to an object but don’t want to prevent it from being garbage - collected. This is useful in caching scenarios.

4. Tune Garbage Collection

  • Choose the Right Garbage Collector: Java provides different garbage collectors, such as Serial GC, Parallel GC, CMS (Concurrent Mark Sweep) GC, and G1 (Garbage - First) GC. You can choose the appropriate garbage collector based on your application’s requirements. For example, if your application has low pause time requirements, G1 GC might be a good choice. You can specify the garbage collector using JVM options, like -XX:+UseG1GC.
  • Adjust Heap Size: You can adjust the initial and maximum heap size of the JVM using the -Xms and -Xmx options. For example, -Xms512m -Xmx1024m sets the initial heap size to 512 MB and the maximum heap size to 1024 MB.

Distributed System

have u work for a multi-regions project? how to ensure the data consistency?

Yeah, I’ve worked on multi-region projects before. To make sure the data stays consistent, we first decided which parts really need strong consistency and which ones are okay with eventual consistency.

  • when we need eventual consistency: we used MongoDB Atlas with Global Clusters—it helped us handle cross-region sync in a flexible way.
  • When we needed strong consistency: we used PostgreSQL with synchronous replication to make sure the data was always accurate.
  • For files, we used S3 replication with versioning so we don’t lose any updates.
  • We also added idempotency on the API level to avoid duplicate writes.

security

what are the scopes in OAuth process

In the OAuth (Open Authorization) process, scopes are used to define the specific permissions that a client application requests from the resource owner. They act as a way to limit the access that the client has to the protected resources of the resource owner. Here are some common aspects and examples of scopes in the OAuth process:

1. General Concept

Scopes are strings that represent a set of related permissions. When a client application initiates the OAuth authorization flow, it includes a list of scopes in the authorization request. The resource owner is then presented with these scopes and can choose to grant or deny the requested permissions. The authorization server issues an access token that is limited to the scopes that the resource owner has approved.

2. Examples of Scopes in Different Services

Social Media Platforms

  • Facebook:
    • email: Allows the client application to access the user’s Facebook - associated email address.
    • user_posts: Grants the application the ability to read the user’s posts on Facebook.
    • publish_pages: Enables the application to publish content on the user’s Facebook pages.
  • Twitter:
    • tweet.read: Permits the application to read the user’s tweets and timeline.
    • tweet.write: Allows the application to post tweets on behalf of the user.
    • users.read: Gives the application access to basic user information such as the user’s profile.

Cloud Storage Services

  • Google Drive:
    • https://www.googleapis.com/auth/drive.readonly: Grants read - only access to the user’s Google Drive files.
    • https://www.googleapis.com/auth/drive: Provides full access to the user’s Google Drive, including read, write, and delete operations.

Email Services

  • Gmail:
    • https://www.googleapis.com/auth/gmail.readonly: Allows the application to read the user’s Gmail messages.
    • https://www.googleapis.com/auth/gmail.send: Enables the application to send emails on behalf of the user.

3. Importance of Scopes

  • Security: Scopes help in enhancing security by ensuring that the client application only has access to the resources and operations that the resource owner explicitly approves. This reduces the risk of a malicious or misconfigured application accessing sensitive information.
  • User Control: They give users more control over their data. Users can review the scopes requested by an application and decide whether to grant the permissions based on their trust in the application.

difference between encryption and hashing

Encryption and hashing are two fundamental concepts in the field of data security, but they serve different purposes and have distinct characteristics. Here’s a detailed comparison between them:

1. Purpose

  • Encryption: Its main purpose is to transform data into an unreadable format so that it can be safely transmitted or stored. The original data, known as plaintext, can be recovered by using a decryption key. This is crucial for protecting sensitive information such as financial transactions, personal data, and confidential communications. For example, when you make an online purchase, your credit card details are encrypted to prevent unauthorized access during transmission.
  • Hashing: Hashing is used to generate a fixed - size string of characters (hash value) from input data. It is mainly employed for data integrity verification and password storage. A hash value can be used to check if the data has been altered, as even a small change in the input data will result in a completely different hash value. Passwords are often hashed so that they can be stored securely without revealing the actual password.

2. Process

  • Encryption: It involves an encryption algorithm and a key. The encryption algorithm takes the plaintext and the key as inputs and produces ciphertext. There are two main types of encryption: symmetric encryption (uses the same key for encryption and decryption, like AES) and asymmetric encryption (uses a pair of keys - a public key for encryption and a private key for decryption, such as RSA).
  • Hashing: A hash function takes the input data and generates a hash value through a one - way process. There is no key involved in the traditional sense. Popular hash functions include MD5, SHA - 1, SHA - 256, and SHA - 512. Once the hash value is generated, it is not possible to reverse - engineer the original input data from the hash.

3. Reversibility

  • Encryption: It is a reversible process. If you have the correct decryption key, you can convert the ciphertext back to the original plaintext. This reversibility is essential for scenarios where the data needs to be accessed in its original form after being encrypted.
  • Hashing: It is a one - way function, which means it is not possible to reverse the process and obtain the original input data from the hash value. The only way to verify if a given input matches the original data is to hash the new input and compare the resulting hash values.

4. Output Size

  • Encryption: The output size (ciphertext) is usually proportional to the size of the input plaintext, with some additional overhead depending on the encryption algorithm. For example, in AES encryption, the output size is the same as the input size for block - based encryption.
  • Hashing: The output size is fixed regardless of the size of the input data. For instance, the output of the SHA - 256 hash function is always 256 bits (32 bytes), whether the input is a single character or a large file.

5. Use Cases

  • Encryption:
    • Secure communication channels (e.g., SSL/TLS for HTTPS)
    • Encrypting data at rest (e.g., encrypting a hard drive or a database)
    • Protecting sensitive files and folders
  • Hashing:
    • Password storage in user accounts
    • Verifying the integrity of downloaded files (e.g., checking the hash of a software download)
    • Blockchain technology for maintaining the integrity of transaction data

SSL 3 ways handshake

Java backend syllabus

see Security - ##SSL 3 ways handshake

misc

online

  • Hash code, hash maps and semaphore …something to look into
  • Write a SQL for all employees with addresses. Read upon outer join. You may need to use outer join to write this query
  • Multithreading and How it is Different from Multitasking
  • How deadlock plays a important role in multithreading
  • Thread program questions:
    • How many ways to Create a thread?
    • can you wrote program to call 2 or more threads at a time..
    • write a program Thread will sleep() and wait() .
  • What is Daemon Thread , how to write Daemon Thread, stream program questions:
    • Wrote program to Sort the list of given strings in increasing order of their length
    • Wrote program to Reverse an integer array
  • Spring basic questions
  • Creating threads
  • Sample code for streams
  • Sample code for hashmaps
  • Functional interfaces
  • concepts of “key” and “value” in a Map
  • Sql query for fetching employee data without address
  • key advantages of using lambda expressions
  • Dependency injection functionality in spring
  • infinite loop and all - basic coding .
  • getMap
  • putMap
  • Write a SQL query for fetching employee data without address
    • Tables:
      • Employee Table: Contains employee information, including employee ID (e.g., employee_id).
      • Address Table: Contains address information, including a foreign key (e.g., employee_id) referencing the employee table.
    • To retrieve employees whose address is null using a FULL OUTER JOIN, you’d typically join the employees table with an addresses table, checking for null values in the address columns of the addresses table within the join condition. If a full outer join is not available, you can achieve the same result using a LEFT JOIN and a WHERE clause to filter for null address values.
      SELECT
      e.employee_id,
      e.first_name,
      e.last_name,
      a.address_id,
      a.street_address,
      a.city
      FROM
      employees e
      LEFT JOIN
      addresses a ON e.address_id = a.address_id
      WHERE
      a.street_address IS NULL;

New York Bank

下面我逐个为你解答这些问题:

1. Change patterns(变更模式)

变更模式通常指软件设计里用于管理和处理系统变更的模式。常见的变更模式有策略模式、观察者模式等。策略模式可让算法的使用和实现分离,在运行时能轻松切换不同算法;观察者模式则能让对象间建立一对多的依赖关系,当一个对象状态改变时,依赖它的对象会收到通知并自动更新。

2. Questions while coding(编码时的问题)

编码时常见问题包括语法错误、逻辑错误、性能问题、依赖冲突等。解决方法有:查看错误提示信息、调试代码、查阅文档、搜索相关论坛或社区。

3. Primitive types(基本数据类型)

不同编程语言基本数据类型有所不同。以 Java 为例,基本数据类型有整数类型(byte、short、int、long)、浮点类型(float、double)、字符类型(char)和布尔类型(boolean)。Python 中虽然没有严格意义上的基本数据类型,但有整数(int)、浮点数(float)、布尔值(bool)、字符串(str)等内置类型。

4. When you work, if you have problems, how do you handle(工作中遇到问题如何处理)

  • 自我排查:查看错误日志、调试代码、查阅文档。
  • 搜索信息:在搜索引擎、技术论坛(如 Stack Overflow)查找解决方案。
  • 请教他人:向同事、导师或技术社区求助。
  • 记录总结:把问题和解决方案记录下来,方便后续查阅和总结经验。

5. SQL - group by roll up(SQL 的 GROUP BY ROLLUP)

GROUP BY ROLLUP 是 SQL 中的一个高级分组功能,它能生成额外的汇总行。例如:

SELECT department, job_title, SUM(salary)
FROM employees
GROUP BY ROLLUP (department, job_title);

此查询会生成按部门和职位分组的汇总数据,还会生成部门级别的汇总数据和全局汇总数据。
SELECT department, job_title, SUM(salary) FROM employees GROUP BY ROLLUP (department, job_title); 这条 SQL 语句运用了 GROUP BY ROLLUP 子句,它是 SQL 里用于高级分组和生成汇总数据的强大功能。下面详细解释此语句的各个部分以及 ROLLUP 的作用。

语句各部分解释

1. SELECT 子句

SELECT department, job_title, SUM(salary)

这部分指定了要从 employees 表中选取的列。

  • department:表示员工所在的部门。
  • job_title:代表员工的职位。
  • SUM(salary):对 salary 列进行求和操作,也就是计算每个分组内员工工资的总和。

2. FROM 子句

FROM employees

此部分指明了数据的来源表,即从 employees 表中获取数据。

3. GROUP BY ROLLUP 子句

GROUP BY ROLLUP (department, job_title)

这是该语句的核心部分,ROLLUP 是一种特殊的分组操作,它会在 GROUP BY 子句的基础上生成额外的汇总行。具体来说,ROLLUP (department, job_title) 会按照以下顺序进行分组:

  1. 首先按照 departmentjob_title 进行分组,计算每个部门和职位组合下的工资总和。
  2. 接着按照 department 进行分组,计算每个部门的工资总和。
  3. 最后生成一个全局汇总行,计算所有员工的工资总和。

示例数据与结果

假设 employees 表有如下数据:

department job_title salary
IT Developer 5000
IT Tester 4000
HR Recruiter 3500
HR Trainer 3000

执行 SELECT department, job_title, SUM(salary) FROM employees GROUP BY ROLLUP (department, job_title); 后,结果可能如下:

department job_title SUM(salary)
IT Developer 5000
IT Tester 4000
IT NULL 9000 – IT 部门的汇总
HR Recruiter 3500
HR Trainer 3000
HR NULL 6500 – HR 部门的汇总
NULL NULL 15500 – 全局汇总

总结

GROUP BY ROLLUP 子句能够生成多层次的汇总数据,对于分析数据很有帮助。在这个例子中,它先按部门和职位分组,再按部门分组,最后生成全局汇总,让你可以从不同粒度查看工资数据。

6. Coding on notepad(在记事本里编码)

在记事本里编码比较基础,适合简单脚本或代码片段的编写。不过它缺乏语法高亮、代码提示等功能,调试和维护代码相对困难。可以考虑使用更专业的代码编辑器,如 Visual Studio Code、PyCharm 等。

7. Multiple questions(多个问题)

处理多个问题时,可按问题的优先级和复杂度逐个解决。先解决关键问题和容易解决的问题,再处理复杂问题。

8. Restful APIs(RESTful API)

RESTful API 是一种遵循 REST 架构风格的 API 设计原则。其特点有:

  • 使用 HTTP 方法:如 GET(获取资源)、POST(创建资源)、PUT(更新资源)、DELETE(删除资源)。
  • 使用 URL 定位资源:例如 /users/1 表示 ID 为 1 的用户。
  • 无状态:每个请求都是独立的,服务器不保存客户端的状态信息。

9. Previous work experience(以往工作经验)

描述以往工作经验时,可从项目背景、担任角色、主要职责、取得成果等方面入手。比如:“在[项目名称]中,我担任[角色],负责[具体工作内容],通过[具体方法],成功[取得的成果]。”

10. Multi threading(多线程)

多线程是指在一个程序里同时执行多个线程。多线程能提高程序的并发性能,尤其适用于 I/O 密集型和 CPU 密集型任务。在 Java 中,可通过继承 Thread 类或实现 Runnable 接口来创建线程。示例代码如下:

class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("线程正在执行");
}
}

public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}

11. Microservices(微服务)

微服务是一种将应用拆分成多个小型、自治服务的架构风格。每个服务都能独立开发、部署和扩展,通过轻量级通信机制(如 HTTP、消息队列)进行交互。微服务架构的优点包括提高开发效率、增强系统的可扩展性和容错性等。

12. Coding - palindrome(回文编码)

回文是指正读和反读都相同的字符串或数字。以下是 Python 实现判断字符串是否为回文的代码:

def is_palindrome(s):
return s == s[::-1]

# 测试
print(is_palindrome("racecar")) # 输出: True
print(is_palindrome("hello")) # 输出: False

13. Compare to method vs some other method(方法比较)

在比较不同方法时,可从以下几个方面考虑:

  • 性能:执行时间、内存占用等。
  • 复杂度:代码复杂度、理解难度等。
  • 适用性:适用场景、数据规模等。

14. Ask for inputs and outputs for coding questions(询问编码问题的输入输出)

在解决编码问题时,明确输入和输出很重要。输入是程序运行所需的数据,输出是程序运行后的结果。例如,对于判断回文的问题,输入是一个字符串,输出是一个布尔值,表示该字符串是否为回文。

15. Design patterns(设计模式)

设计模式是软件开发中针对反复出现问题总结出的通用解决方案。常见的设计模式有单例模式、工厂模式、观察者模式等。单例模式确保一个类只有一个实例,并提供全局访问点;工厂模式用于创建对象,将对象的创建和使用分离。

16. API gateways(API 网关)

API 网关是一个位于客户端和后端服务之间的中间层,负责接收客户端的请求,对请求进行路由、转换、过滤、认证等处理,然后将请求转发到相应的后端服务,并将后端服务的响应返回给客户端。API 网关可以提高系统的安全性、可维护性和可扩展性。

17. Book - gang of 4 - the person who designed the design patterns(《设计模式:可复用面向对象软件的基础》作者)

《设计模式:可复用面向对象软件的基础》(Design Patterns: Elements of Reusable Object - Oriented Software)通常被称为“四人组(Gang of Four)”的书,作者是 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides。

18. Fast paced questions(快速提问)

面对快速提问,要保持冷静,集中注意力,迅速理解问题的核心,然后有条理地给出回答。如果问题复杂,可以先简要说明思路,再详细解释。

Spring Boot 是一个用于简化 Spring 应用开发的框架,它提供了自动配置、嵌入式服务器等功能,能让开发者快速搭建和部署 Spring 应用。例如,使用 Spring Boot 开发一个简单的 Web 应用:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HelloWorldApplication {

@GetMapping("/")
public String hello() {
return "Hello, World!";
}

public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}

20. Array and list(数组和列表)

数组和列表都是用于存储多个元素的数据结构,但它们有一些区别:

  • 数组:大小固定,元素类型相同,访问速度快。在 Java 中,数组定义如下:

    int[] array = new int[5];
  • 列表:大小可变,元素类型可以不同,插入和删除操作比较方便。在 Java 中,常用的列表实现类是 ArrayList

    import java.util.ArrayList;
    import java.util.List;

    public class Main {
    public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    }
    }

21. Code - implement an api, get a user id, delete by id(实现一个 API,获取用户 ID 并根据 ID 删除用户)

以下是使用 Spring Boot 实现该 API 的示例代码:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@RestController
public class UserApiApplication {

private static final Map<Integer, String> users = new HashMap<>();

static {
users.put(1, "Alice");
users.put(2, "Bob");
}

@DeleteMapping("/users/{id}")
public ResponseEntity<String> deleteUser(@PathVariable int id) {
if (users.containsKey(id)) {
users.remove(id);
return new ResponseEntity<>("User deleted successfully", HttpStatus.OK);
} else {
return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
}
}

public static void main(String[] args) {
SpringApplication.run(UserApiApplication.class, args);
}
}

22. Data grouping(数据分组)

在 SQL 中,可使用 GROUP BY 子句进行数据分组。例如,按部门分组统计员工数量:

SELECT department, COUNT(*)
FROM employees
GROUP BY department;

在 Python 中,可使用 pandas 库进行数据分组:

import pandas as pd

data = {
'department': ['HR', 'IT', 'HR', 'IT'],
'name': ['Alice', 'Bob', 'Charlie', 'David']
}

df = pd.DataFrame(data)
grouped = df.groupby('department').count()
print(grouped)

23. Million record file(百万条记录的文件)

处理百万条记录的文件时,要考虑内存使用和处理效率。可以采用逐行读取的方式,避免一次性将整个文件加载到内存中。以下是 Python 逐行读取文件的示例:

with open('large_file.txt', 'r') as file:
for line in file:
# 处理每一行数据
print(line)

24. Array copy method - list implementation(数组复制方法 - 列表实现)

在 Java 中,可使用 Arrays.copyOf 方法复制数组:

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
int[] copied = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copied));
}
}

在 Python 中,可使用切片操作复制列表:

original = [1, 2, 3, 4, 5]
copied = original[:]
print(copied)

25. Gets the array and copies it over(获取数组并复制)

参考上面的数组复制方法示例。

26. Backend(后端)

后端开发负责处理业务逻辑、数据存储和管理等。常见的后端技术栈有 Java + Spring Boot、Python + Django/Flask、Node.js + Express 等。

27. Kafka(Kafka)

Kafka 是一个分布式流处理平台,常用于处理高吞吐量的实时数据流。它具有高吞吐量、可扩展性、持久性等特点。Kafka 的主要组件有生产者(Producer)、消费者(Consumer)、主题(Topic)和分区(Partition)。

28. Rabbit MQ(RabbitMQ)

RabbitMQ 是一个开源的消息队列中间件,遵循 AMQP(高级消息队列协议)标准。它可用于实现异步通信、解耦系统组件等。RabbitMQ 的主要组件有生产者、消费者、交换机(Exchange)、队列(Queue)和绑定(Binding)。

29. Dataflow in your project(项目中的数据流)

描述项目中的数据流时,可从数据的来源、处理过程和去向等方面入手。例如,数据从前端表单收集,经过后端服务器的业务逻辑处理,存储到数据库中,然后根据需求从数据库中读取数据并展示到前端页面。

30. Design pattern used in your project(项目中使用的设计模式)

在项目中,可根据具体需求使用不同的设计模式。例如,使用单例模式确保数据库连接池只有一个实例,使用工厂模式创建不同类型的对象。

31. Exception handling(异常处理)

异常处理是指在程序运行过程中捕获和处理异常情况,避免程序崩溃。在 Java 中,可使用 try-catch-finally 语句进行异常处理:

try {
int result = 1 / 0;
} catch (ArithmeticException e) {
System.out.println("发生算术异常: " + e.getMessage());
} finally {
System.out.println("无论是否发生异常,都会执行这里的代码");
}

在 Python 中,可使用 try-except-finally 语句进行异常处理:

try:
result = 1 / 0
except ZeroDivisionError:
print("发生除零异常")
finally:
print("无论是否发生异常,都会执行这里的代码")

32. Framework rules(框架规则)

不同框架有不同的规则和使用方式。使用框架时,要熟悉框架的文档和规范,按照框架的规则进行开发。例如,使用 Spring Boot 时,要遵循其自动配置、注解使用等规则。

33. Saga pattern microservices(微服务中的 Saga 模式)

Saga 模式是一种用于处理分布式事务的模式,适用于微服务架构。当一个业务流程涉及多个服务的操作时,Saga 模式通过一系列的本地事务来实现最终一致性。每个本地事务都有一个对应的补偿事务,当某个本地事务失败时,会执行相应的补偿事务来撤销之前的操作。

34. SQL injection - user form using delete from DB(使用用户表单进行 SQL 注入 - 从数据库删除数据)

SQL 注入是一种常见的安全漏洞,攻击者可通过构造恶意的 SQL 语句来执行非法操作。例如,在用户表单中输入恶意的 SQL 语句,如 '; DELETE FROM users; --,可能会导致数据库中的用户数据被删除。为防止 SQL 注入,可使用参数化查询(如在 Java 中使用 PreparedStatement):

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {
public static void main(String[] args) {
String userId = "1";
String sql = "DELETE FROM users WHERE id = ?";
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, userId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

35. DB indexes(数据库索引)

数据库索引是一种数据结构,用于提高数据库查询的效率。常见的索引类型有 B - 树索引、哈希索引等。通过在经常用于查询条件的列上创建索引,可以减少数据库的扫描范围,加快查询速度。但索引也会增加数据库的存储空间和写操作的开销,因此需要根据实际情况合理创建索引。

36. Springboot annotation(Spring Boot 注解)

Spring Boot 提供了许多注解,用于简化开发。常见的注解有:

  • @SpringBootApplication:用于标记主应用类,开启自动配置、组件扫描等功能。
  • @RestController:用于标记控制器类,将其方法返回值直接作为 HTTP 响应体返回。
  • @GetMapping@PostMapping 等:用于映射 HTTP 请求方法。

37. Palindrome logic(回文逻辑)

参考前面的回文编码部分。

38. Biggest number of array(数组中的最大数)

以下是 Python 实现找出数组中最大数的代码:

numbers = [1, 5, 3, 9, 2]
max_number = max(numbers)
print(max_number)

在 Java 中:

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] numbers = {1, 5, 3, 9, 2};
int max = Arrays.stream(numbers).max().getAsInt();
System.out.println(max);
}
}

39. Logic questions - write code(逻辑问题 - 编写代码)

请明确具体的逻辑问题,以便我为你编写相应的代码。

onsite

  1. 之前项目daily job是什么
  2. 从一个ticket开始到结束的过程是怎么样的,开发上线之后需要做什么,有没有什么monitor和logging工具,了解splunk吗
    3.介绍一下项目架构,用的什么技术栈
  3. 后端数据库用什么连接,hibernate用了什么缓存,用了什么注释。缓存和直接取数据的区别
  4. 用过哪些java version,有啥feature,知不知道某个feature(没听出来是啥),garbage collector是如何工作的
  5. 发请求后security用了什么,用OAuth2时是怎么用的
  6. join和join fetch的区别,解释left join是什么,写一个join语句
  7. 如何优化query效率,在hibernate中如何实现pagination,java的pagination对象
  8. restful api传输的什么,在项目中是如何序列化和反序列化的,项目中在哪些layer上用的restful api
  9. 用多个数据库的情况,如何管理不同的connection
  10. SPA是什么,为什么需要用到异步函数,了解回调函数吗,为啥要用回调函数
  11. elasticsearch中header需要什么
  12. 为啥要用kafka,kafka的结构,partition是什么
  13. 什么是localthread,localthread和synchronized的区别,一边loop一边修改会发生什么,如何避免concurrent exception,iterator和map的区别,为啥concurrenthashmap可以避免,flatmap和map的区别,stream api有哪些方法,
  14. git相关,介绍你如何使用git,什么是remote和local。如果push到错误的分支怎么办
  15. 介绍什么是@transactional,为什么要用这个注释

coding

  • find kth element in an array
  • find the middle node in a linked list
  • leetcode 767: reorganize string
  • lc 3given a string s, find the length of the longest substring without duplicate characters.
  • There is a company which has a CEo and a hierarchy of employees. All employees have a unique iD, name and a pointer to their manager and their reports.4 //Please implement the whoIsOurBoss() method to find the closest manager for two given employees (i.e. the manager farthest from the CEo that both employees report up to).