BackEnd Flashcards

1
Q

What is a code block? What are some examples of a code block?

A
  1. lexical structure of source code that are grouped together. Code Blocks consist of one or more declarations and statements. (lexiscope refers to setting the scope, or range of functionality, of a variable so that it may be called (referenced) from within the block of code in which it is defined.)
  2. In JavaScript, code blocks are denoted by curly braces { }.
  3. For example, if else, for, do while, while and so on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does block scope mean?

A

Block scope means the variable definition is only valid within the block of code that it was declared in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the scope of a variable declared with const or let?

A

const and let variables are Block-Scoped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between let and const?

A

Let can be reassigned and Const cannot.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is it possible to .push() a new value into a const variable that points to an Array?

A

It is possible to .push() to a new value into a const variable, because it is adding to the array instead of reassigning the variable.

The values in the array are mutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How should you decide on which type of declaration to use?

A

If the variable does not needed to be reassigned, then use const

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the syntax for writing a template literal?

A
  1. put it inside backticks (``)
  2. ${ } holds the expressions and variables

example:
let name = ‘sharon’;
let age = 26;

phrase = hi my name is ${ name } and I am ${ age } years old;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is “string interpolation”?

A

String formatting: the ability to substitute part of the string for the values of variables or expressions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is restructuring conceptually?

A

Taking out part of an object and assigning it to new variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax for Object destructuring?

A

const { title, author, libraryID } = book1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the syntax for Array destructuring?

A

const [ book3, book4, book5 ] = getBooks()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you tell the difference between destructuring and creating Object/Array literals

A

the side of the assignment operator that the object or array is on.

creating object : def is right side of the equal sign
restructuring: { } and property name is on the left side of the =

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax for defining an arrow function?

A

functionName = parameter => {
code block
}

functionName = () => { }

() not needed with one parameter
() are needed with no parameter

{} needed for more than one line of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When an arrow function’s body is left without curly braces, what changes in its functionality?

A

You don’t get a return statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is the value of this determined within an arrow function?

A

this is defined at definition time for arrow functions. In other words, THIS is defined within the same lexiscope or parentscope.

For other functions, this is determined at call time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Node.js?

A

Node.js is a program that allows JavaScript to be run outside of a web browser.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What can Node.js be used for?

A

Building back ends for Web applications, command-line programs, or any kind of automation that developers wish to perform.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a REPL?

A

A read–eval–print loop (REPL), also termed an interactive top-level or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise.

Chrome Dev Tools Console is one example.

This is more like a playground for testing things.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When was Node.js created?

A

May 27, 2009

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What back end languages have you heard of?

A

Node, Python, PHP, Ruby

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the process object in a Node.js program?

A

• The process object is a global
• that global informs about and controls the current Node.js process.
• As a global, it is always available to Node.js applications without using require().
• It can also be explicitly accessed using require():

const process = require(‘process’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you access the process object in a Node.js program?

A

Just type process, it is always available. Treat it like any other object.

It is a global so you can access it anywhere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the data type of process.argv in Node.js?

A

Array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is a JavaScript module?

A

Module in Node.js is a functionality organized in single or multiple JavaScript files that are reused.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What values are passed into a Node.js module’s local scope?

A

exports,
require,
module,
__filename,
__dirname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Give two examples of truly global variables in a Node.js program.

A

Console, process, global

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the purpose of module.exports in a Node.js module?

A

It exports a module so other modules can use it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How do you import functionality into a Node.js module from another Node.js module?

A

Using the require function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the purpose of module.exports in a Node.js module?

A

Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How do you import functionality into a Node.js module from another Node.js module?

A

Blocking assignment executes “in series” because a blocking assignment blocks execution of the next statement until it completes.
Non-blocking assignment executes in parallel because it describes assignments that all occur at the same time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is a directory?

A

An organizational structure and location for storing files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is a relative file path?

A

The location that is relative to where the file is.

./fileName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is an absolute file path?

A

The path of the file from the root directory. (This is not a good idea to use because what is an absolute file path on my computer might not be the same for someone else’s computer).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What module does Node.js include for manipulating the file system?

A

the fs module

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What method is available in the Node.js fs module for writing data to a file?

A

fs.writeFile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Are file operations using the fs module synchronous or asynchronous?

A

Both, since there are different sync versions, but fs.writeFile is asynchronous Asynchronous take callback arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is npm?

A

npm is the world’s largest software registry. It can be used to share and borrow packages.
It has three components:
1. the website
2. the Command Line Interface (CLI)
3. the registry

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What is a package?

A

a package is a package or module of code to reuse. It needs to be a directory with one or more files. It has a package.JSON file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

How can you create a package.json with npm?

A

Run the “npm init –yes” command on the command line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is a dependency and how to you add one to a package?

A

dependencies are the modules that the project relies on to function properly.

npm install

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What happens when you add a dependency to a package with npm?

A

The newly installed package(s) gets added to the list of dependencies in your package. json file

It downloads the packages that it depends on until it has everything.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

How do you add express to your package dependencies?

A

run this command in the terminal while you are in the package directory:

npm install express

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What Express application method starts the server and binds it to a network PORT?

A

listen( ) method.

app.listen([port [, host [, backlog] ] ][, callback])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is a client?

A

a client is a piece of computer hardware or software that accesses a service made available by a server as part of the client–server model of computer networks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is a server?

A

a server is a piece of computer hardware or software (computer program) that provides functionality for other programs or devices, called “clients”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Which HTTP method does a browser issue to a web server when you visit a URL?

A

GET

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What is on the first line of an HTTP request message?

A

1.An HTTP method,
2. The request target,
3. The HTTP version

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What is on the first line of an HTTP response message?

A

The protocol version, A status code, A status text.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

What are HTTP headers?

A

It is meta-information about the request or describes the body included in the messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Is a body required for a valid HTTP message?

A

No. It is optional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

How do you mount a middleware with an Express application?

A

Middleware functions are functions that have access to the request object (req), the response object (res), They can be mounted with app.use

app.use((req, res) => { }
“the use method of the app object is being called with ONE argument with TWO PARAMETERS: req and use.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?

A

the request and response objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is the appropriate Content-Type

A

application/JSON

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What is the significance of an HTTP request’s method?

A

It tells what function your want to run in the code such as deleting and getting data in app.delete( ) and app.get( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What does the express.json() middleware do and when would you need it?

A

parses incoming request bodies if payload is JSON. It is needed when content type header is application/json in the HTTP request.

In other words, it is a function or a program (or something) that is going to run between the time that the server gets the request and the time that the server sends the request out to the client. It is a function that happens between the beginning of the response and sending of the response.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

What is PostgreSQL and what are some alternative relational databases?

A

PostgreSQL is a powerful, free, open source Relational Database Management System (RDBMS). Other popular relational databases include MySQL (also free), SQL Server by Microsoft, and Oracle by Oracle Corporation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What are some advantages of learning a relational database?

A

they can be modeled well, data corruption is very unlikely, categorize data, and they are widely used.

58
Q

What is one way to see if PostgreSQL is running?

A

running this command in the terminal:

sudo service postgresql status

59
Q

What is SQL and how is it different from languages like JavaScript?

A

SQL is different! SQL is a declarative programming language. In declarative languages, programmers describe the results they want and the programming environment comes up with its own plan for getting those results.JavaScript is imperative

60
Q

How do you retrieve specific columns from a database table?

A

select “name”, “other names”

61
Q

How do you filter rows based on some specific criteria?

A

where “category” = ‘cleaning’;

62
Q

What are the benefits of formatting your SQL?

A

makes it easier to read, understand, and debug it.

63
Q

What are four comparison operators that can be used in a where clause?

A
  1. Not equals
  2. Greater than / greater than or equal to
  3. lesser than / lesser than or equal to
  4. Inclusive of two values (BETWEEN…AND)
64
Q

How do you limit the number of rows returned in a result set?

A

limit 1; at end of code
(the limit clause then followed by the number)

65
Q

How do you retrieve all columns from a database table?

A

Select*

66
Q

How do you control the sort order of a result set?

A

order by priceDESC if you want descending instead of ascending

order by clause, then DESC for descending order. The default is ascending order.

67
Q

What is SQL and how is it different from languages like JavaScript?

A

SQL is different! SQL is a declarative programming language. In declarative languages, programmers describe the results they want and the programming environment comes up with its own plan for getting those results.JavaScript is imperative

68
Q

How do you retrieve specific columns from a database table?

A

select “name”

69
Q

How do you filter rows based on some specific criteria?

A

use where class then the name of the column wrapped in double quotes, equal operator and then the value of the category wrapped in single quotes.

where “category” = ‘cleaning’

70
Q

What are the benefits of formatting your SQL?

A

makes it easier to read, organize, and debug

71
Q

What are four comparison operators that can be used in a where clause?

A
  1. greater than or greater than or equal to
  2. less than, less than or equal to
  3. not equals to !=
  4. equals to =
72
Q

How do you limit the number of rows returned in a result set?

A

limit 1;

limit clause, then number of what your limit is but it is always at end of code.

73
Q

How do you retrieve all columns from a database table?

A

select *

74
Q

How do you control the sort order of a result set?

A

order the data by adding the keyword DESC at the end if your code if you want descending order.
The default will always be ascending order.

75
Q

How do you add a row to a SQL table?

A

INSERT INTO “products” (“name”, “description”, “price”, “category”)
VALUES (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’);

76
Q

What is a tuple?

A

In SQL, a list of values is referred to as a tuple.

77
Q

How do you add multiple rows to a SQL table at once?

A

insert into “products” (“name”, “description”, “price”, “category”)values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’), (‘Tater Mitts’, ‘Scrub some taters!’, 6, ‘cooking’)returning *;

78
Q

How do you get back the row being inserted into a table without a separate select statement?

A

If you only want specific values back, you can use a comma-separated list of column names instead of an * asterisk for the returning statement

79
Q

What is a foreign key?

A

The column that links two tables together since it is in both tables

80
Q

How do you join two SQL tables?

A

select “products”.”name” as “product”, “suppliers”.”name” as “supplier” from “products” join “suppliers” using (“supplierId”);

81
Q

How do you temporarily rename columns or tables in a SQL statement?

A

Table aliasing likeselect “p”.”name” as “product”, “p”.”category”, “s”.”name” as “supplier”, “s”.”state” from “products” as “p” join “suppliers” as “s” using (“supplierId”);

82
Q

What are some examples of aggregate functions?

A

sum, avg, count

83
Q

What is the purpose of a group by clause?

A

when you want to separate rows into groups and perform aggregate functions on those groups of rows. This is done with a group by clause.

84
Q

What are the three states a Promise can be in?

A
  1. pending: initial state, neither fulfilled nor rejected.
  2. fulfilled: meaning that the operation was completed successfully.
  3. rejected: meaning that the operation failed.
85
Q

How do you handle the fulfillment of a Promise?

A

attach a then method handler with callback function with success message

86
Q

How do you handle the rejection of a Promise?

A

attach a catch method handler with callback function with error message

87
Q

What is Array.prototype.filter useful for?

A

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

88
Q

What is Array.prototype.map useful for?

A

The map() method creates a new array populated with the results of calling a provided function on EVERY element in the calling array.

89
Q

What is Array.prototype.reduce useful for?

A

Reduces an array of values to a smaller or one value.

90
Q

What is “syntactic sugar”?

A

In computer science, syntactic sugar is syntax within a programming language that makes things easier to read or to express. It makes the language “sweeter” for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

Syntactic sugar is usually a shorthand for a common operation that could also be expressed in an alternate, more verbose, form: The programmer has a choice of whether to use the shorter form or the longer form, but will usually use the shorter form since it is shorter and easier to type and read.

91
Q

What is the typeof an ES6 class?

A

function

92
Q

Describe ES6 class syntax.

A

class key word and curly braces:
class ExampleClass { }
const example = new ExampleClass { }

class key word, name of function, function code block, constructor inside with arguments and then function code block:

class Student {
constructor( arguments, arguments) {
this.firstName = firstName;
this.lastName = lastName;
this.subject = subject;
}
}

93
Q

What is “refactoring”?

A

It changes the structure of the code by cleaning and reorganizing it without affecting or changing the functionality of it.

94
Q

What is Webpack?

A

Webpack bundles multiple modules into a single JavaScript file.
It is a tool that lets you bundle your JS applications and it can support many different assets such as images, fonts, and stylesheets.

95
Q

How do you add a devDependency to a package?

A

$ npm install <package-name> --save-dev</package-name>

96
Q

What is an NPM script?

A

something that automates repetitive tasks in package.json file, it is a command line command under script in the package.json

97
Q

How do you execute Webpack with npm run?

A

npm run


98
Q

How are ES Modules different from CommonJS modules?

A

import/export for ES6 Modules.

modules.export are for CommonJS.
import uses require for Common JS

99
Q

What kind of modules can Webpack support?

A

webpack supports the following module types natively: ECMAScript modules, CommonJS modules, and AMD modules.

100
Q

What is React?

A

A framework that manages UI’s based off of UI components. It helps us build websites quicker.

101
Q

What is a React element?

A

An object that describes a virtual DOM node.

102
Q

How do you mount a React element to the DOM?

A

**You can tell React to “mount” it into a DOM container by calling the .createRoot( ) Method on the ReactDOM object.

const root = ReactDOM.createRoot(container);

Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.

const container = document.querySelector(‘#root’);
const root = ReactDOM.createRoot(container);

103
Q

What is Babel?

A

Babel is a toolchain that translates ECMAScript 2015+ code into a backwards compatible version of JavaScript.

104
Q

What is a Plug-in?

A

A Plug-in is a software component that adds a specific feature to an existing computer program and enables customization.

105
Q

What is a Webpack loader?

A

Loaders are transformations that are applied to the source code of a module. They allow you to pre-process files as you import or “load” them. Thus, loaders are kind of like “tasks” in other build tools and provide a powerful way to handle front-end build steps. Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs. Loaders even allow you to do things like import CSS files directly from your JavaScript modules!

106
Q

How can you make Babel and Webpack work together?

A

Add this to the webpack.config.js file:
in the use: { } :

loader: ‘babel-loader’,
options: {
plugins: [
‘@babel/plugin-transform-react-jsx’
]

107
Q

What is JSX?

A

A syntax extension to JavaScript, it produces react elements and lets you use HTML syntax in JavaScript

108
Q

Why must the React object be imported when authoring JSX in a module?

A

If you give some JSX to Babel, you will see that JSX is just sugar for React.createElement calls. This is why we need to import React if we use JSX.

109
Q

How can you make Webpack and Babel work together to convert JSX into valid JavaScript?

A

Add this to the webpack.config.js file:

loader: ‘babel-loader’,
options: {
plugins: [
‘@babel/plugin-transform-react-jsx’
]

110
Q

What is a React component?

A

React components lets you split the UI into independent, reusable pieces and think about each pieces in isolation.

111
Q

How do you define a function component in React?

A

function CustomButton( [props] ) {
return <button>Click Me!</button>
};

112
Q

How do you mount a component to the DOM?

A

mount by createRoot method of the ReactDOM object and then rendering the root object:
const root =
ReactDOM.createRoot(document.querySelector(‘#root’));
// const element = <CustomButton></CustomButton>;
// root.render(element);
root.render(<CustomButton></CustomButton>);

113
Q

What are props in React?

A

props are objects that get passed to a React element.

114
Q

How do you pass props to a component?

A

You pass props as attributes to the component itself. Example:

const element = (

<div>
<CustomButton></CustomButton>
<CustomButton></CustomButton>
<CustomButton></CustomButton>
</div>

);

115
Q

How do you write JavaScript expressions in JSX?

A

use curly braces that wrap around the JSX expressions!

function CustomButton(props) {
return <button>{ props.text }</button>;
}

116
Q

How do you create “class” component in React?

A
  1. class keyword.
  2. function name starting with a capitalized letter.
  3. extends keyword.
  4. React.Component { }
  5. render() in function code block.
  6. return React element statement.

class CustomButton extends React.Component {
render() {
return <button<{ this.props.text }</button>;
}
}

117
Q

How do you access props in a class component?

A

{this.props.text} inside the return statement of the React element.

118
Q

What is the purpose of state in React?

A

React components has a built-in state object. The state object is where you store property values that belongs to the component. When the state object changes, the component re-renders.State is like our data model for React, we use it to determine what the user should see when it renders.

119
Q

How to you pass an event handler to a React element?

A

return <button onCLick={ this.handleClick }>Click me!</button>

you put the event handler on the react element.

120
Q

What are controlled components?

A

In HTML, form elements such as , , and typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with setState().We can combine the two by making the React state be the “single source of truth”. Then the React component that renders a form also controls what happens in that form on subsequent user input. An input form element whose value is controlled by React in this way is called a “controlled component”.A react component that reacts to the changes of an element that it is wrapped around.It receives it value via state and it has a change handler that updates that state.

121
Q

What two props must you pass to an input for it to be “controlled”?

A

value prop and an onChange prop

122
Q

What Array method is commonly used to create a list of React elements?

A

.map()

123
Q

What is the best value to use as a “key” prop when rendering lists?

A

The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys.

124
Q

What does express.static() return?

A

it returns a function.

125
Q

What is the local __dirname variable in a Node.js Module?

A

The directory name of the current module.

This is the same as the path.dirname() of the __filename.It starts with / so it is absolute file path since it starts at the root of the file system

126
Q

What does the join() method of Node’s path module do?

A

The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.

127
Q

What does fetch() return?

A

Returns a promise containing the response (a Response Object)

128
Q

What is the default request method used by fetch()?

A

GET

129
Q

How do you specify the request method (GET, POST, etc.) when calling fetch?

A

The fetch() method can optionally accept a second parameter, an init

130
Q

When does React call a component’s componentDidMount method?

A

After render, but before page is finished loading

131
Q

Name 3 React Component lifecycle methods.

A
  1. constructor
  2. render
  3. componentDidMount
132
Q

How do you pass data to a child component?

A

as a prop to the child component

133
Q

What does the acronym LIFO mean?

A

Last in, first out.

134
Q

What methods are available on a Stack data structure?

A

.push() to push onto the top of the stack
.pop() to pop the last one off.

a stack should always have a push or pop().

.peek() and print().

135
Q

What must you do to access the value at an arbitrary point in a queue (not just the “front”)?

A

Do the pop() method to take off the top value of the stack and keep popping off the value until you react that arbitrary point.

136
Q

What does the acronym FIFO mean?

A

First in, First out

137
Q

What methods are available on a Stack data structure?

A

enqueue(value) - adds a value to the “back” of the queue.

dequeue() - removes the “front” value from the queue and returns it.

Together, these facilitate first-in-first-out (FIFO) operations: the first thing enqueued onto the queue is the first thing that can be dequeued out.
Often, queues include additional helper operations that make them a bit easier to use, such as peek(), which returns the “front” value of the queue without removing it.

138
Q

What must you do to access the value at an arbitrary point in a queue (not just the “front”)?

A

Must enqueue(value) or dequeue until you reach that arbitrary point in the queue.

139
Q

How are linked lists different from an array?

A

Linked lists are sequential access (like a queue), but not a random access (like an array).

140
Q

How would you access an arbitrary node in a linked list (not just the “head”)?

A

In order to go to a specific place in the list, you have to start at the beginning, then jump from node to node until you get there. However, unlike a queue, a linked list doesn’t need to be mutated to scan its contents.

In a basic linked list, you start at the “head” node, then jump to the next node through the .next property. The last node in a basic linked list is called the “tail” node. The “list” itself is simply a reference to the “head” node. Since each node only contains a reference to the next node in the list, there is no way to backtrack. That means each node is the “head” of its own list, and there’s no way to tell if any other nodes came before it.*