Skip to content

Instantly share code, notes, and snippets.

@stanleykerr
Last active November 15, 2022 21:47
Show Gist options
  • Save stanleykerr/af76ee768f2744002be9df841efa0cf6 to your computer and use it in GitHub Desktop.
Save stanleykerr/af76ee768f2744002be9df841efa0cf6 to your computer and use it in GitHub Desktop.
TestDome Questions
class Node {
public int value;
public Node left, right;
public Node(int value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
}
}
public class BinarySearchTree {
public static boolean contains(Node root, int value) {
return root.value == value || (root.value < value ? root.right != null && contains(root.right, value) : root.left != null && contains(root.left, value));
}
public static void main(String[] args) {
Node n1 = new Node(1, null, null);
Node n3 = new Node(3, null, null);
Node n2 = new Node(2, n1, n3);
System.out.println(contains(n2, 3));
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Company page</title>
</head>
<body>
<p>Welcome! Here you can find the following things:</p>
<ol>
<li><em><a href="#logo">Company's logo</a></em></li>
<li><a href="#employees">List of employees</a></li>
</ol>
<h1>Company's logo</h1>
<p>Company uses the following logos:</p>
<ul>
<li>New logo:<img src="new_logo.gif"/></li>
<li>Old logo:<img src="old_logo.gif"/></li>
</ul>
<h1>List of employees</h1>
<table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Mary</td>
<td>Williams</td>
</tr>
<tr>
<td>James</td>
<td>Smith</td>
</tr>
</tbody>
</table>
</body>
</html>
class Input extends React.PureComponent {
render() {
let {forwardedRef, ...otherProps} = this.props;
return <input {...otherProps} ref={forwardedRef} />;
}
}
const TextInput = React.forwardRef((props, ref) => {
return <Input {...props} forwardedRef={ref} />
});
class FocusableInput extends React.Component {
ref = React.createRef()
render() {
return <TextInput ref={this.ref} />;
}
componentDidUpdate(prevProps) {
if(prevProps.focused != this.props.focused && this.props.focused) {
this.ref.current.focus();
}
}
componentDidMount() {
if(this.props.focused)
this.ref.current.focus();
}
}
FocusableInput.defaultProps = {
focused: false
};
const App = (props) => <FocusableInput focused={props.focused} />;
document.body.innerHTML = "<div id='root'></div>";
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
const Product = props => {
const plus = () => props.onVote(1);
const minus = () => props.onVote(-1);
return (
<li>
<span>{props.name}</span> - <span>votes: {props.votes}</span>
<button onClick={plus}>+</button>{" "}
<button onClick={minus}>-</button>
</li>
);
};
class GroceryApp extends React.Component {
constructor(props) {
super(props);
this.state = {
products: props.products
};
}
onVote = (dir, index) => {
let products = [...this.state.products];
products[index].votes += dir;
this.setState({ products });
};
render() {
return (
<ul>
{this.state.products.map((p, index) => <Product key={`p-${index}`} onVote={(dir) => this.onVote(dir, index)} {...p} />)}
</ul>
);
}
}
document.body.innerHTML = "<div id='root'></div>";
ReactDOM.render(<GroceryApp
products={[
{ name: "Oranges", votes: 0 },
{ name: "Apples", votes: 0 },
{ name: "Bananas", votes: 0 }
]}
/>, document.getElementById('root'))
-- Counts the amount of students who are named John
SELECT COUNT(id)
FROM students
WHERE firstName="John";
import java.util.stream.Stream;
public class MergeNames {
public static String[] uniqueNames(String[] names1, String[] names2) {
return Stream.of(names1, names2).flatMap(Stream::of).distinct().toArray(String[]::new);
}
public static void main(String[] args) {
String[] names1 = new String[] {"Ava", "Emma", "Olivia"};
String[] names2 = new String[] {"Olivia", "Sophia", "Emma"};
System.out.println(String.join(", ", MergeNames.uniqueNames(names1, names2))); // should print Ava, Emma, Olivia, Sophia
}
}
public class Palindrome {
public static boolean isPalindrome(String word) {
return new StringBuilder(word).reverse().toString().equalsIgnoreCase(word);
}
public static void main(String[] args) {
System.out.println(Palindrome.isPalindrome("Deleveled"));
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Spreadsheet</title>
<style>
td {
text-align: right;
width: 33%;
}
td, th, table {
border: 1px solid;
border-collapse: collapse;
}
th {
text-align: left;
}
</style>
</head>
<body>
<table>
<caption>Purchase Orders</caption>
<thead>
<tr>
<th>Order Date</th>
<th>SKU</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td>07-16-2018</td>
<td>523402</td>
<td>54</td>
</tr>
</tbody>
</table>
</body>
</html>
-- Selects userId and average session duration where user has multiple sessions
SELECT
userId,
AVG(duration) AS averageSessionDuration
FROM sessions
GROUP BY userId
HAVING COUNT(*)>1;
// Converts dates from M/D/YYYY to YYYYMMDD
function formatDate(userDate) {
return new Date(userDate).toLocaleDateString('ko-KR', { month: "2-digit", day: "2-digit", year: "numeric" }).replace(/\.(\s)?/g, "");
}
console.log(formatDate("12/31/2014"));
function createCheckDigit(membershipId) {
let val = [...String(membershipId)].reduce((a, b) => Number(a) + Number(b));
return val < 10 && val || createCheckDigit(val);
}
console.log(createCheckDigit("55555")); // returns 7
function ensure(value) {
if (arguments[0] === undefined) throw new Error("No arguments passed");
return value;
}
// e.g.
ensure("test"); // returns "test"
ensure(); // throws error
-- Selects item name and seller name for each item that belongs to a seller with a rating greater than 4.
SELECT items.name, sellers.name
FROM items INNER JOIN sellers
ON items.sellerId = sellers.id
WHERE sellers.rating > 4;
import java.util.Map;
import java.util.HashMap;
public class TwoSum {
public static int[] findTwoSum(int[] list, int sum) {
if (list == null || list.length < 2) return null;
Map<Integer, Integer> indexMap = new HashMap<>();
for (int i = 0; i < list.length; i++) {
int v = list[i];
int needed = sum - v;
if (indexMap.containsKey(needed))
return new int[] { indexMap.get(needed), i };
indexMap.put(v, i);
}
return null;
}
public static void main(String[] args) {
int[] indices = findTwoSum(new int[] { 3, 1, 5, 7, 5, 9 }, 10);
if(indices != null) {
System.out.println(indices[0] + " " + indices[1]);
}
}
}
public class UserInput {
public static class TextInput {
private StringBuilder input = new StringBuilder();
public void add(char c) {
this.input.append(c);
}
public String getValue() {
return input.toString();
}
}
public static class NumericInput extends TextInput {
@Override
public void add(char c) {
if(!Character.isDigit(c)) return;
super.add(c);
}
}
public static void main(String[] args) {
TextInput input = new NumericInput();
input.add('1');
input.add('a');
input.add('0');
System.out.println(input.getValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment