1 / 12

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.

laksha
Download Presentation

Moderate Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Moderate Problem

  2. Problem • Write a function to swap a number in place without temporary variables.

  3. 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:

  4. 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”

  5. 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.

  6. 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 • }

  7. 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} )

  8. 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

  9. 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.

More Related