Uploaded by Faiz Mohammed

code

advertisement
Round 1
1. Given an input string s, reverse the order of the words. (10:45 to
11.00)
A word is defined as a sequence of non-space characters. The words
in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single
space.
Input: s = "the sky is blue"
Output: "blue is sky the"
Program: (C++)
string reverseWords(string s) {
int n = s.size();
string ans = "";
for (int i=n-1; i>=0; i--){
if (s[i] != ' '){
int end = i;
while (i>=0 && s[i] != ' '){
i--;
}
int start = i+1;
if (ans == "") ans = ans + s.substr(start, end-start+1);
else ans += ' ' + s.substr(start, end-start+1);
}
}
return ans;
}
Time Complexity: O(N)
Space Complexity: O(N) - space is required to return the output
2. Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules: (14:00 to
14:30)
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the digits 1-9 must occur exactly once in each of the 9 3x3
sub-boxes of the grid.
The '.' character indicates empty cells.
Example 1:
Input: board =
[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],["."
,"9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",
".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".","
.",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8
",".",".","7","9"]]
Output:
[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","
8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","
2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","
8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","
9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
Program: (C++)
void solveSudoku(vector<vector<char>>& board) {
solve(board);
}
bool isValid(vector<vector<char>>&board, int row, int col, int c){
for (int i=0; i<9; i++){
if (board[row][i] == c) return false;
if (board[i][col] == c) return false;
if (board[3*(row/3)+i/3][3*(col/3)+i%3] == c) return false;
}
return true;
}
bool solve(vector<vector<char>> & board){
for (int i=0; i<9; i++){
for (int j = 0; j<9; j++){
if (board[i][j] == '.'){
for (char c='1'; c<='9'; c++){
if(isValid(board,i,j,c)){
board[i][j] = c;
if (solve(board)) return true;
board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
Time Complexity: O(1)
Space Complexity: O(1)
3. Write an algorithm to determine if a number n is happy. (12:00 to
12:24)
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of
the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or
it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Sample input :
n = 19
Sample output :
True
Program: (C++)
int next(int n){
int temp = 0;
while (n>0){
int dig = n%10;
temp += dig*dig;
n/=10;
}
return temp;
}
bool isHappy(int n) {
int slow = n, fast = n;
do {
slow = next(slow);
fast = next(next(fast));
} while (slow != fast);
return slow == 1;
}
Time Complexity: O(N)
Space Complexity: O(1)
4. Given a string s containing just the characters '(' , ')' , '{' , '}' , '[' and
']' , determine if the input string is valid. (12:30 to 12:56)
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same
type.
Sample input:
s = "()[]{}"
Sample output:
True
Program: (C++)
bool isValid(string s) {
stack<int> st;
for (int i=0; i<s.size(); i++){
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
st.push(s[i]);
else if (st.empty() || abs(s[i]-st.top()) > 2)
return false;
else
st.pop();
}
return st.empty();
}
Time Complexity O(N)
Space Complexity O(N)
Download