Easy Flashcards

1
Q

Given a 32-bit signed integer, reverse digits of an integer.

A

check return 0 part

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

Given an array, rotate the array to the right by k steps, where k is non-negative.

A

Take care of–>

k=k%nums.length

and

nums.length - 1

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

Given two binary trees, write a function to check if they are the same or not.

A

recursion

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

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

A

Time complexity : O(n) We traverse the list containing n elements only once. Each look up in the table costs only O(1) time.

Space complexity : O(n)

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

how to find if key is present in map or not

A

map.containsKey(key)

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

how to create and initialize array

A

int[] ret = new int [] {m.get(target- nums[i]), i};

int[] ret = new int [] {a, b};

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

How to throw invalid argument exeption

A

throw new IllegalArgumentException(“No two sum solution”);

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

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

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

pass by value n reference in java

A

pass by value- for all java primitive data types like int, float, boolean

pass by reference- for all non primitive data types like arrays, stack, etc

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

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

A

Maintain two stacks.

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

Given a linked list, determine if it has a cycle in it.

A

Time complexity : O(n)

Space complexity : O(1)

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

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

A

public boolean isValid(String s) {

Stack<character> stack = new Stack<character>();</character></character>

for (char c : s.toCharArray()) {

if (c == ‘(‘)

stack.push(‘)’);

else if (c == ‘{‘)

stack.push(‘}’);

else if (c == ‘[’)

stack.push(‘]’);

else if (stack.isEmpty() || stack.pop() != c) return false;

}

return stack.isEmpty();

}

Time complexity : O(n)

push and pop operations on a stack take O(1) time.

Space complexity :O(n)

in the worst case, we will end up pushing all the brackets onto the stack. e.g. ((((((((((.

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

Remove Duplicates from Sorted Array

Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn’t matter what values are set beyond the returned length.

A

Time complextiy : O(n). Assume that n is the length of array. Each of i and j traverses at most n steps.

Space complexity : O(1).

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

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.

A

Time complexity should be O(n)

space O(1)

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

Input: 1->2->4, 1->3->4

Output: 1->1->2->3->4->4

A

Time complexity : O(n + m)

Space complexity : O(1)

——————————————recursion——————————

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}

Space complexity : O(n + m)

Time complexity : O(n + m)

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

Move Zeroes

Input: [0,1,0,3,12]

Output: [1,3,12,0,0]

A

Space Complexity : O(1)

Time Complexity: O(n)

17
Q

Invert Binary Tree

A

Since each node in the tree is visited only once, the time complexity is O(n), where n is the number of nodes in the tree.

Because of recursion,O(h) function calls will be placed on the stack in the worst case, where h is the height of the tree. Because h∈O(n), the space complexity is O(n)

18
Q

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

Example:

MovingAverage m = new MovingAverage(3);

m. next(1) = 1
m. next(10) = (1 + 10) / 2
m. next(3) = (1 + 10 + 3) / 3
m. next(5) = (10 + 3 + 5) / 3

A
19
Q

Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.

Logger logger = new Logger();

// logging string “foo” at timestamp 1 logger.shouldPrintMessage(1, “foo”); returns true;

// logging string “bar” at timestamp 2 logger.shouldPrintMessage(2,”bar”); returns true;

// logging string “foo” at timestamp 3 logger.shouldPrintMessage(3,”foo”); returns false;

// logging string “bar” at timestamp 8 logger.shouldPrintMessage(8,”bar”); returns false;

// logging string “foo” at timestamp 10 logger.shouldPrintMessage(10,”foo”); returns false;

// logging string “foo” at timestamp 11 logger.shouldPrintMessage(11,”foo”); returns true;

A
20
Q

We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same.

Each node has another two boolean attributes : isLeaf and val. isLeaf is true if and only if the node is a leaf node. The val attribute for a leaf node contains the value of the region it represents.

Your task is to use a quad tree to represent a given grid.

A

class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;

public Node() {}

public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
};

21
Q

Pascal’s Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.

A
22
Q
A