Moderate Problem
Download
Report
Transcript Moderate Problem
Moderate Problem
Problem
Write a function to swap a number in place without
temporary variables.
Hints
This is a classic interview problem.
If you haven’t heard this problem before, you can
approach it by taking the difference between a and b:
Problem
Given an integer between 0 and 999,999, print an English
phrase that describes the integer
eg, “One Thousand, Two Hundred and Thirty Four”
Hints
This is not an especially challenging problem, but it is a
long and tedious one.
Your interviewer is unlikely to ask to see every detail, but
he / she will be interested in how you approach the
problem.
Solution
public static String numtostring(int num) {
}
String[] wordarr1 = {“”,”One ”, “Two ”, “Three ”, “Four ”, “Five ”, “Six ”, “Seven ”,
“Eight ”,”Nine ”};
String[] wordarr11 = {“”, “Eleven ”, “Twelve ”, “Thirteen ”, “Fourteen ”, “Fifteen ”,
“Sixteen ”, “Seventeen ”, “Eighteen ”, “Nineteen ”};
String[] wordarr10 = {“”,”Ten ”, “Twenty ”, “Thirty ”, “Forty ”, “Fifty ”, “Sixty ”,
“Seventy ”, “Eighty ”, “Ninety “};
String[] wordarr100 = {“”, “Hundred ”, “Thousand ”};
// Count number of digits in num.
Init results
If num is zero {do something }
If num is between 0- 9
if num is between 10-99
If num is between 100-999
If num is > than 999
Problem
You are given an array of integers (both positive and
negative).Find the continuous sequence with the largest
sum.Return the sum.
EXAMPLE
Input: {2, -8, 3, -2, 4, -10}
Output: 5 (i.e., {3, -2, 4} )
hints
A simple linear algorithm will work by keeping track of
the current subsequence sum. If that sum ever drops
below zero, that subsequence will not contribute to the
subsequent maximal subsequence since it would reduce it
by adding the negative sum.
MinMax is one of the positive number
In previous example, minMax=4
Problem
Design an algorithm to find all pairs of integers within an
array which sum to a specified value.
One easy and (time) efficient solution involves a hash map
from integers to integers. This algorithm works by
iterating through the array. On each element x, look up
sum - x in the hash table and, if it exists, print (x, sum x).Add x to the hash table, and go to the next element.