In this question, we are given an array of integer nums and an integer target, and we are asked to return two distinct indices such that the numbers in those positions add up to be the target. The question assumes that there is one and only one solution for each input.

Approach 1: Brute Force

Traverse the array. For each number num in the array, traverse the rest of the array to see if target - num can be found.

For example:

Image for post
Image for post

Java implementation:

For time complexity, let n be the length of the integer array. In the outer loop, we traverse each…


Var

Using var declares either a locally(within function) scoped variable or a globally scoped variable.

Scope

When var is used outside a function, it declares a globally scoped variable.

var a = 2; // globally scoped variablefunction foo() {
console.log(a);
}
foo(); // prints out 2console.log(window.a); // prints out 2

This is similar to adding a property to window object directly, while there is a difference: you can’t delete the variable declared using var.

var a = 2; // globally scoped variablewindow.b = 3; // new property of window object, also globally scopeddelete a; // returns falsedelete…

Image for post
Image for post

In today’s Executive Order on Addressing the Threat Posed by WeChat, WeChat is gonna to be banned in 45 days. In this story I will talk about what will, probably, happen to the WeChat users who are currently residing in the States after it is banned?

I’m an international student from China currently pursuing my master’s degree in the States and I’m also a heavy user of WeChat. Everyday I text my friends and family in China using WeChat. I also text most of the Chinese international students here with it.

It’s really hard to imagine the life without WeChat…


Typically you can invoke your API using a URL of the following format after deploying:

https://api-id.execute-api.region.amazonaws.com/stage

Even though the region and stage are relatively stable and easy to remember, the api-id is kinda like a random number for each API created and is really not user friendly when it comes to memorizing. It would be great if we can invoke the API using a custom domain name, for example:

https://www.example.com/myapi

or something like

https://myapi.example.com

I have an API Gateway API which triggers a Lambda function to serve Gatsby site and backend APIs and a domain registered with Route53. My goal…


Image for post
Image for post

Deploying a multi-page Gatsby app to Lambda can be tricky, as you may find that the root URL works fine, while it breaks as soon as you click on some internal link trying to navigate to a different page within your app.

In this article, I will demonstrate how to deploy a multi-page Gatsby app to Lambda with the help of serverless, serverless-http and Express.js.

Create a Gatsby Site

For the demonstration purpose, I’ll use one of the Gatsby official starter — gatsby-starter-blog-theme. You can also use whatever Gatsby project you have at hand.

  1. Breate a Gatsby site with gatsby-starter-blog-theme:
# create a new…

I’m currently working on an API website that provides users with APIs to retrieve movie information in JSON format. The very first challenge I met when designing the overall architecture was how can I serve my front-end web pages together with backend APIs under the same URL, i.e. if a user enters

https://www.dummy.com/

the server will return the website home page so user can signup for a free API key/ browse document to learn how to use the API; if a user enters

https://www.dummy.com/?key=API_KEY&field1=value1&field2=value2

the server will return corresponding JSON data/error message based on the query strings user supplies.

My…


Connecting to DynamoDB from Lambda is even easier than you can think of.

Firstly, you need to create an execution role which later you can attach to the lambda function. To do so:

  1. Login IAM, select Roles under Access management in the left sidebar, click on the Create role button.
  2. In the Select type of trusted entity section, select AWS service and select Lambda in the Choose a use case section.

3. Select a policy/policies you want to attach to this role, you can also create a customized policy to restrict the access permission other than granting write/read permission on…


By default AWS managed policies like AmazonDynamoDBFullAccess and AmazonDynamoDBReadOnlyAccess allow corresponding rules to all tables in DynamoDB, while we may want to select specific access permissions and apply to a specific table/tables.

This is where customer managed policies come into play. We can create a customized policy and attach it to a specific user group.

First, login IAM. On the left sidebar, select Policies under Access management, then click on Create policy button.

Image for post
Image for post

Click on Choose a service, type DynamoDB and select it in the result list.


Preparation

Create a User and a Group

The first question I had right after I created a table in DynamoDB was: wait, there was no prompt for creating user and password. How can I connect to it later in the code?

The answer is you have to create a user and password for your DynamoDB in IAM (Identity and Access Management). You can imagine IAM as a central system that manages all the credentials like username, access key, secret key, etc. for each of the services you use in AWS.

  1. Sign in to AWS console.
  2. Enter “IAM” in the search box under Find Services, click on it…

Wenhe Qi

A lifelong learner who has enthusiasm for sharing knowledge; a developer who loves bringing ideas to life.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store