Epic Systems Interview Question for Software Developers


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

//////////////////////////////////////
import java.util.ArrayList;
import java.util.List;

public class Solution {
	public List<Long> allFibNumbers(long m, long n){
		List<Long> list = new ArrayList<Long>();
		String s = String.valueOf(n);
		long i = 1;
		while(String.valueOf(i).length() * 2 < s.length()){
			long[] dp = new long[s.length()];
			dp[0] = i;
			dp[1] = i;
			String temp = String.valueOf(dp[0]) + String.valueOf(dp[1]);
			for(int j = 2; j < dp.length; j++){
				dp[j] = dp[j - 1] + dp[j - 2];
				temp += String.valueOf(dp[j]);
				if(Long.valueOf(temp).longValue() < n && Long.valueOf(temp).longValue() > m){
					list.add(Long.valueOf(temp).longValue());
				}
				if(Long.valueOf(temp).longValue() > n) break;
			}
			i++;
		}
		return list;
	}
}
/////////////////////////////

- Boyang Li March 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*IN JAVA*/

import java.util.ArrayList;
		import java.util.Arrays;
		import java.util.List;

		public class Test {

			public static void main(String[] args) {

				find(1000, 9999);

			}

			public static void find(int min, int max) {
				
				System.out.println("Finding between "+min+" and " +max+"......");

				List<Integer> numList = new ArrayList<Integer>();

				for (int i = min; i <= max; i++) {

					String strNumber = String.valueOf(i);
					
					try {

						if (strNumber.length() >= 3) {
							String strleft = "", strRight = "";
							int range = (strNumber.length() / 3);
							
							for (int j = 0; j < range; j++) {
								strleft += String.valueOf(strNumber.charAt(j));
								strRight = strNumber.substring(j + 1, j + 1 + strleft.length());
								
								if (strleft.equals(strRight)) {
									int num = Integer.valueOf(strleft) + Integer.valueOf(strRight);
									
									String strNum = String.valueOf(num);
									if (strNumber.length() >= j + 1 + strleft.length() + strNum.length()) {
										if (Integer.valueOf(strNumber.substring(j + 1 + strleft.length(), j + 1 + strleft.length() + strNum.length())) == num) {
											
											numList.add(i);
											break;
										}
									}
								}

							}

						}

						
					} catch (Exception e) {
						System.err.println("Number: " + strNumber);
						e.printStackTrace();
					}

				}

				System.out.println("Number Matched: "+Arrays.toString(numList.toArray())+ "\n~~~ D O N E  ~~~");
				

			}

		}

- anij0009 February 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is this correct?... Your program is outputting 1120 where 1+1=2 but 1+2!=0. But the question is sum of the previous 2 equals to that number.

- Venu February 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Venu : I guess any one instance of the condition is enough to satisfy.
As given in question, 121224 has only one instance of the above condition matching.
last three numbers 2+2 = 4 satisfies the condition, but the first three numbers doesn't.

- senthilmpro February 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

C solution

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

typedef struct {
    int * p_corner;
    int max_hor;
    int max_dia_l;
    int max_dia_r;
    int max_ver;
} small_m;

int main(int argc, char * argv[]){

    int curr_val;
    int i,k,inc;
    
    uint64_t small_bound = atoi(argv[1]);
    uint64_t large_bound = atoi(argv[2]);

    int start_digit = log10(small_bound);
    int num_of_digits = log10(large_bound) - start_digit;

    if( start_digit < 4 || small_bound > large_bound){
        printf("ERROR with args\n");
        return 0;
    }

    printf("These are your lucky numbers between %llu and %llu\n", small_bound, large_bound);


    for(k = 0; k <= num_of_digits; k++){
        curr_val = pow(10, start_digit+k) + 12*pow(10, start_digit-2+k);
        inc = 112*pow(10, start_digit-2+k);
        
        for( i = 0; i < 4; i++){

            printf("%d\n", curr_val);
            curr_val += inc;
        }        
    }
    return 1;


}

- DeRock February 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Console C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("X");
            find(1000,9999);
            int x = Console.Read();
        }
        public static void find(int min, int max)
{
	if (min >= max)
		return;
	for (int i = min; i <max; i++)
	{
		string num = i.ToString();
		if (num.Length < 3)
		continue;
		if (num[0] == num [1])
		{
            int sum = (int)Char.GetNumericValue(num[0]) + (int)Char.GetNumericValue(num[1]);
			if(sum<10)
			{
				if (num[2].ToString() == sum.ToString())
				{
					Console.WriteLine(i);
					continue;
				}
				
			}
			else
			{
				if(num.Length >= 4)
				{
					string temp = num[2].ToString() + num[3];
					if (temp == sum.ToString())
					{
						Console.WriteLine(i);
						continue;
					}
				}
				else
				{
					continue;
				}

			}
			
		}
		
	}
	
}
    }
}

- Anonymous February 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;

import java.util.Scanner;

public class question2 {

	public static void main(String[] args) {

		Scanner Scan = new Scanner(System.in);
		int min,max;
		System.out.println("Enter min:");
		min= Scan.nextInt();
		max = Scan.nextInt();
		Scan.close();
		for (int i=min;i<=max;i++)
		{
			String number = String.valueOf(i);
			String num =number;
			int firstIndex=0,inc=1;
			String first= String.valueOf(number.charAt(firstIndex));
			String second= String.valueOf(number.charAt(firstIndex+1));
			firstIndex+=1;
			while(true)
			{
				if(first.equals(second)||firstIndex>=(number.length()-1))
					break;
				first+=second;
				inc=first.length();
				second="";

				for(int j=0;j<inc;j++){

					firstIndex++;
					if(firstIndex<=(number.length()-1)){
						second+= String.valueOf(number.charAt(firstIndex));
					}
				}
			}
			if(first.equals(second)){
				while(true){

					int lenF=first.length();
					int lenNum= number.length();
					int x=(lenNum-(2*lenF));
					if(x<lenF)
					{			
						break;
					}else
					{
						String rnumber=number.substring(first.length()+second.length());

						int sum = Integer.parseInt(first)+Integer.parseInt(second);
						int res = 0,lenOfresult=String.valueOf(sum).length();

						if(lenOfresult<=rnumber.length())
							res = Integer.parseInt(rnumber.substring(0,lenOfresult));
						if(res!=sum){
							break;
						}
						else{
							first=second;
							number=second+rnumber;
							second = String.valueOf(res);
							if(rnumber.length()==lenOfresult)
							{
								System.out.println(num);
								break;
							}
						}
					}

				}

			}

		}


	}


}

- Karthik February 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ModifiedFib {
private int MAX;
private int MIN;
public ModifiedFib(int min, int max)
{
this.MIN=min;
this.MAX=max;
}
public void generateFib(int n1,int n2, String newNum)
{
int num=n1+n2;
if(newNum.length()==0)
{
newNum=newNum+""+n1+""+n2+""+num;
} else {
newNum=newNum+num;
}
int value=Integer.parseInt(newNum);
if(value>MAX)
{
return;
}
if(value>=MIN && value<=MAX)
System.out.print(newNum+" ");
generateFib(n2,num,newNum);
}

public static void main(String[] args) {
ModifiedFib mFib=new ModifiedFib(1000, 10000000);
for(int i=1;i<mFib.MAX;i++)
{
mFib.generateFib(i,i, "");
}
}

- Mahbubur February 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ModifiedFib {
	private int MAX;
	private int MIN;
	public ModifiedFib(int min, int max)
	{
		this.MIN=min;
		this.MAX=max;
	}
	public void generateFib(int n1,int n2, String newNum)
	{
		int num=n1+n2;
		if(newNum.length()==0)
		{
			newNum=newNum+""+n1+""+n2+""+num;
		} else {
			newNum=newNum+num;
		}
		int value=Integer.parseInt(newNum);
		if(value>MAX)
		{
			return;
		}
		if(value>=MIN && value<=MAX)
			System.out.print(newNum+" ");
		generateFib(n2,num,newNum);
	}
	
	public static void main(String[] args) {
		ModifiedFib mFib=new ModifiedFib(1000, 10000000);
		for(int i=1;i<mFib.MAX;i++)
		{
			mFib.generateFib(i,i, "");
		}
	}

- Mahbubur February 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class numberSumPattern
{

public static void chkVals(int min, int max) {
int digBlocksLen = 0;
int digBlocks = 0;
int[] num = new int[3];
for(int i = min; i<=max; i++) {
if(max < 100 || min < 0 || min > max) return;
String intString = Integer.toString(i);
digBlocksLen = intString.length();

if ((digBlocksLen % 3 == 1) || (digBlocksLen % 3 == 0)) {
digBlocks = digBlocksLen/3;

for(int j=0; j<2; j++){
String a = intString.substring(j*digBlocks,(j+1)*digBlocks);

num[j]=Integer.parseInt(a);

}

int j=2;
num[j] = Integer.parseInt(intString.substring(j*digBlocks,digBlocksLen));
//System.out.println("num values "+intString.substring(j*digBlocks-1,digBlocksLen));
if(chkCondition(num)) System.out.println("Detected "+intString);
}
}
}

public static Boolean chkCondition(int[] x) {

if (x[1] != x[0]) return false;
if (x[2] == x[1]+x[0]) { return true; }
return false;
}

public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
System.out.println("Shouldn't work");
chkVals(10,30);
System.out.println("Should work");
chkVals(100,3000);
System.out.println("Should work");
chkVals(100000,500000);
}
}
}

- Divya February 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{((
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class numberSumPattern
{

public static void chkVals(int min, int max) {
int digBlocksLen = 0;
int digBlocks = 0;
int[] num = new int[3];
for(int i = min; i<=max; i++) {
if(max < 100 || min < 0 || min > max) return;
String intString = Integer.toString(i);
digBlocksLen = intString.length();

if ((digBlocksLen % 3 == 1) || (digBlocksLen % 3 == 0)) {
digBlocks = digBlocksLen/3;

for(int j=0; j<2; j++){
String a = intString.substring(j*digBlocks,(j+1)*digBlocks);

num[j]=Integer.parseInt(a);

}

int j=2;
num[j] = Integer.parseInt(intString.substring(j*digBlocks,digBlocksLen));
//System.out.println("num values "+intString.substring(j*digBlocks-1,digBlocksLen));
if(chkCondition(num)) System.out.println("Detected "+intString);
}
}
}

public static Boolean chkCondition(int[] x) {

if (x[1] != x[0]) return false;
if (x[2] == x[1]+x[0]) { return true; }
return false;
}

public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
System.out.println("Shouldn't work");
chkVals(10,30);
System.out.println("Should work");
chkVals(100,3000);
System.out.println("Should work");
chkVals(100000,500000);
}
}
}}}

- Divya February 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
	
	public static boolean testCond(int num, int length)
	{
		String s = String.valueOf(num);
		if (s.length()<3*length) return false;
		if (Integer.parseInt(s.substring(0,length))!=Integer.parseInt(s.substring(length,2*length))) return testCond(num,length+1);;
		int i=0;
		for (;i<=s.length()-3*length;i++)
			if (Integer.parseInt(s.substring(i,i+length))+Integer.parseInt(s.substring(i+length,i+2*length))!=Integer.parseInt(s.substring(i+2*length,i+3*length)))
				return testCond(num,length+1);
				
			return true||testCond(num,length+1);
	}
	
	public static void main(String[] args) {
		int min=100;
		int max=242449;
		for (int i=min;i<=max;i++)
			if(testCond(i,1)==true) System.out.println(i);	
	}
}

- S Venu Madhav Chitta February 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Youre code seems broken. What about the number 9918?
All the numbers where the third sequence has one extra digit than the previous sequences are left out by your code!
Another examples are 5510, 6612,7714....

- jainAnks February 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
	
	public static boolean testCond(int num, int length)
	{
		String s = String.valueOf(num);
		if (s.length()<3*length) return false;
		if (Integer.parseInt(s.substring(0,length))!=Integer.parseInt(s.substring(length,2*length))) return testCond(num,length+1);;
		int i=0;
		for (;i<=s.length()-3*length;i++)
			if (Integer.parseInt(s.substring(i,i+length))+Integer.parseInt(s.substring(i+length,i+2*length))!=Integer.parseInt(s.substring(i+2*length,i+3*length)))
				return testCond(num,length+1);
				
			return true||testCond(num,length+1);
	}
	
	public static void main(String[] args) {
		int min=100;
		int max=242449;
		for (int i=min;i<=max;i++)
			if(testCond(i,1)==true) System.out.println(i);	
	}
}

- Venu Madhav Chitta February 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;


public class Criteria {
	public static ArrayList<Integer> produceSeries(int start, int end) {
		ArrayList<Integer> array = new ArrayList<Integer>();
		for(int i=start; i<end; i++) {
			if(checkCriteria(i)) {
				array.add(i);
			}
		}
		return array;
	}
	public static boolean checkCriteria(int num) {
		String str = String.valueOf(num);
		if(str == null || str.length() == 1 || str.length() == 2) {
			return false;
		}
		for(int i=0; i<str.length(); i++) {
			if(i == 0) {
				if(str.charAt(i) != str.charAt(i+1)) {
					return false;
				}
			}
			else if(i >= 2) {
				int a = str.charAt(i);
				a = a -48;
				int b = str.charAt(i-1);
				b = b - 48;
				int c = str.charAt(i-2);
				c = c - 48;
				if(a != b+c) {
					return false;
				}
				
			}
		}
		return true;
	}
	public static void main(String args[]) {
		ArrayList<Integer> array = produceSeries(1000, 9999);
		System.out.println(array);
	}
}

- Anirudh February 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The questions doesn't make sense to me. How does 121224 fall into this category? And also I see some programs compute like 44812 where the adding of 4+8 is represented as 12 in the next two digits. This is an assumption and not giving in the question, only the immediate next element should be the addition of the previous two digits. If the sum exceeds 9 then that number should be ignored.

- Vijay February 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am also confused with what the question is asking for??

- Anu February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question is misleading, that's why! I read the right question elsewhere. The actual question is you can have first two sequences of numbers the same and their addition should be the next sequence. For example, 121224 (12-12-24) has the first two sequences equal(12) and the next sequence is the sum of them(24).

- Anonymous February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

12 + 12 = 24.

- Anony March 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A clearer, faster solution in Java:

public static void printRange(int min, int max) {

		// make sure range is at least 112, since this is the smallest initial number
		if (min < 112) return;

		// make a loop for the numbers themselves, which cannot be greater than the floor of range/100
		String numS;
		int sum, number1, number2;
		long currentNumber;
		for (int i = 1; i < Math.floor(max / 100); i++) {
			number1 = i;
			number2 = i;
			sum = number1 + number2;

			// create the current number
			numS = "" + number1 + number2 + sum;
			currentNumber = Long.parseLong(numS);

			// stop if this number is greater than the max
			if (currentNumber > max) break;

			// otherwise, begin printing it out all variations within range
			while (currentNumber >= min && currentNumber <= max) {
				System.out.print(numS + ", ");
				number1 = sum;
				sum = number1 + number2;
				numS += sum;
				currentNumber = Long.parseLong(numS);
			}
		}
	}
}

- Jeremy Gilreath February 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printRange(int min, int max) {

		// make sure range is at least 112, since this is the smallest initial number
		if (min < 112) return;

		// make a loop for the numbers themselves, which cannot be greater than the floor of range/100
		String numS;
		int sum, number1, number2;
		long currentNumber;
		for (int i = 1; i < Math.floor(max / 100); i++) {
			number1 = i;
			number2 = i;
			sum = number1 + number2;

			// create the current number
			numS = "" + number1 + number2 + sum;
			currentNumber = Long.parseLong(numS);

			// stop if this number is greater than the max
			if (currentNumber > max) break;

			// otherwise, begin printing it out all variations within range
			while (currentNumber >= min && currentNumber <= max) {
				System.out.print(numS + ", ");
				number1 = sum;
				sum = number1 + number2;
				numS += sum;
				currentNumber = Long.parseLong(numS);
			}
		}
	}
}

- Jeremy Gilreath February 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Jeremy, The if(min < 112) should be removed. Since the range can start from 0 and still you have to print the numbers till the end. If you give the range as (0,9999) the result is empty. Except for that the code works fine! Thank you.

- VJ February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also, the addition of two numbers will not be greater than max/1000. You can optimize the current code by doing that change.

- VJ February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops, looking closer into it there is a bug. 11234 gets printed. It should be 11235.

- VJ February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Updated code. I think this works fine. Let me know if it doesn't for any input.

public class PracticeAlgo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		printRange(0, 999999);

	}

	
	public static void printRange(int min, int max) {

		// make a loop for the numbers themselves, which cannot be greater than the floor of range/100
		String numS;
		int sum, number1, number2;
		int currentNumber;
		for (int i = 1; i <= Math.floor(max / 1000); i++) { 
			number1 = i;
			number2 = i;
			sum = number1 + number2;

			// create the current number
			numS = "" + number1 + number2 + sum;
			currentNumber = Integer.parseInt(numS);

			// stop if this number is greater than the max
			if (currentNumber > max) break;

			// otherwise, begin printing it out all variations within range
			while (currentNumber >= min && currentNumber <= max) {
				System.out.print(numS + ", ");
				if(number1 + number2 > 9)
					break;
				int[] nums = giveLastTwoSeqnNumbers(currentNumber, String.valueOf(i).length());
				number1 = nums[0];
				number2 = nums[1];
				sum = number1 + number2;
				numS += sum;
				currentNumber = Integer.parseInt(numS);
			}
		}
	}
	
	public static int[] giveLastTwoSeqnNumbers(int num, int n)
	{
		String numStr = num+"";
		String numStr1 = numStr.substring(numStr.length()-n, numStr.length());
		String numStr2 = numStr.substring(numStr.length()-(2*n), numStr.length()-n);
		int[] nums = {Integer.parseInt(numStr1),Integer.parseInt(numStr2)};
		return nums;
	}
}

- VJ February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fixed another bug in my above code.

public class PracticeAlgo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		printRange(0, 999999);

	}

	
	public static void printRange(int min, int max) {

		// make a loop for the numbers themselves, which cannot be greater than the floor of range/100
		String numS;
		int sum, number1, number2;
		int currentNumber;
		for (int i = 1; i <= Math.floor(max / 1000); i++) { 
			number1 = i;
			number2 = i;
			sum = number1 + number2;

			// create the current number
			numS = "" + number1 + number2 + sum;
			currentNumber = Integer.parseInt(numS);

			// stop if this number is greater than the max
			if (currentNumber > max) break;

			// otherwise, begin printing it out all variations within range
			while (currentNumber >= min && currentNumber <= max) {
				System.out.print(numS + ", ");
				
				int[] nums = giveLastTwoSeqnNumbers(currentNumber, String.valueOf(sum).length(), String.valueOf(number2).length());
				number1 = nums[0];
				number2 = nums[1];
				sum = number1 + number2;
				numS += sum;
				currentNumber = Integer.parseInt(numS);
			}
		}
	}
	
	public static int[] giveLastTwoSeqnNumbers(int num, int n1, int n2)
	{
		String numStr = num+"";
		String numStr1 = numStr.substring(numStr.length()-n1, numStr.length());
		String numStr2 = numStr.substring(numStr.length()-(n2+n1), numStr.length()-n1);
		int[] nums = {Integer.parseInt(numStr1),Integer.parseInt(numStr2)};
		return nums;
	}
}

- VJ February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A typo, should be max/100,

- Patrick February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another bug in my code. Fixed that in below code.

public class PracticeAlgo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		printRange(0, 999999);

	}

	
	public static void printRange(int min, int max) {

		// make a loop for the numbers themselves, which cannot be greater than the floor of range/100
		String numS;
		int sum, number1, number2;
		int currentNumber;
		for (int i = 1; i <= Math.floor(max / 1000); i++) { 
			number1 = i;
			number2 = i;
			sum = number1 + number2;

			// create the current number
			numS = "" + number1 + number2 + sum;
			currentNumber = Integer.parseInt(numS);

			// stop if this number is greater than the max
			if (currentNumber > max) break;

			// otherwise, begin printing it out all variations within range
			while (currentNumber >= min && currentNumber <= max) {
				System.out.print(numS + ", ");
				
				int[] nums = giveLastTwoSeqnNumbers(currentNumber, String.valueOf(sum).length(), String.valueOf(number2).length());
				number1 = nums[0];
				number2 = nums[1];
				sum = number1 + number2;
				numS += sum;
				currentNumber = Integer.parseInt(numS);
			}
		}
	}
	
	public static int[] giveLastTwoSeqnNumbers(int num, int n1, int n2)
	{
		String numStr = num+"";
		String numStr1 = numStr.substring(numStr.length()-n1, numStr.length());
		String numStr2 = numStr.substring(numStr.length()-(n2+n1), numStr.length()-n1);
		int[] nums = {Integer.parseInt(numStr1),Integer.parseInt(numStr2)};
		return nums;
	}
}

- VJ February 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi VJ,
if the input is (0,9999999), it will turn out a bug(NumberFormatException). Could you fix it?

- Tony April 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another python solution (not 100% optimized)

#!/usr/bin/python

import sys
import math
from time import sleep


def num_gen(low, high):
    i = 1
    while i < high:
        d1 = i
        d2 = i
        num = d1
        while True:

            mul = max(10, math.pow(10, math.ceil(math.log(d2 + 1, 10))))
            num = num * mul + d2
            t = d2
            d2 = d1 + d2
            d1 = t

            #print num, mul, d1, d2

            if num < low:
                continue
            if num > high:
                break
            yield num
        i += 1


for i in num_gen(100000, 1000000):
    print int(i)

- JayDee February 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Optimized version:

For a range in 1 billion it takes around 36 ms

#!/usr/bin/python

import sys
import math
from time import sleep
import time


def num_gen(low, high):
    i = 1

    run = True
    while i < high and run:
        run = False
        d1 = i
        d2 = i
        num = d1

        while True:

            mul = max(10, math.pow(10, math.ceil(math.log(d2 + 1, 10))))
            num = num * mul + d2
            t = d2
            d2 = d1 + d2
            d1 = t

            #print num, mul, d1, d2

            if num < low:
                run = True
                continue
            if num > high:
                break
            run = True
            yield num
        i += 1


ms1 = int(round(time.time() * 1000))
j = 0
for i in num_gen(0, 1000000000):
    print i
    j += 1
print j
ms2 = int(round(time.time() * 1000))

print "Time taken = ", (ms2 - ms1)

- JayDee February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Solution:

public static void printPatterns(int min, int max) {
		if(min > max) return;
		int i = 1;
		boolean patternsFound = true;

		while(patternsFound) patternsFound = printPatternsFor(min, max, i++);
	}

	public static boolean printPatternsFor(int min, int max, int starter) {
		int num1 = starter;
		int num2 = starter;
		int temp;
		boolean found = false;
		StringBuffer sb = new StringBuffer();
		sb.append(Integer.toString(starter));
		sb.append(Integer.toString(starter));

		while(Integer.parseInt(sb.toString()) < min) {
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
		if(Integer.parseInt(sb.toString()) <= max) found = true;
		while(Integer.parseInt(sb.toString()) <= max) {
			System.out.println(sb.toString());
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
	
		return found;
	}

	
	public static void main(String[] args) {
		printPatterns(0, 300000);

}

- JRS February 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Solution:

public static void printPatterns(int min, int max) {
		if(min > max) return;
		int i = 1;
		boolean patternsFound = true;

		while(patternsFound) patternsFound = printPatternsFor(min, max, i++);
	}

	public static boolean printPatternsFor(int min, int max, int starter) {
		int num1 = starter;
		int num2 = starter;
		int temp;
		boolean found = false;
		StringBuffer sb = new StringBuffer();
		sb.append(Integer.toString(starter));
		sb.append(Integer.toString(starter));

		while(Integer.parseInt(sb.toString()) < min) {
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
		if(Integer.parseInt(sb.toString()) <= max) found = true;
		while(Integer.parseInt(sb.toString()) <= max) {
			System.out.println(sb.toString());
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
	
		return found;
	}

	
	public static void main(String[] args) {
		printPatterns(0, 300000);
	}

- JRS February 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printPatterns(int min, int max) {
		if(min > max) return;
		int i = 1;
		boolean patternsFound = true;

		while(patternsFound) patternsFound = printPatternsFor(min, max, i++);
	}

	public static boolean printPatternsFor(int min, int max, int starter) {
		int num1 = starter;
		int num2 = starter;
		int temp;
		boolean found = false;
		StringBuffer sb = new StringBuffer();
		sb.append(Integer.toString(starter));
		sb.append(Integer.toString(starter));

		while(Integer.parseInt(sb.toString()) < min) {
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
		if(Integer.parseInt(sb.toString()) <= max) found = true;
		while(Integer.parseInt(sb.toString()) <= max) {
			System.out.println(sb.toString());
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
	
		return found;
	}

	
	public static void main(String[] args) {
		printPatterns(0, 300000);
	}
public static void printPatterns(int min, int max) {
		if(min > max) return;
		int i = 1;
		boolean patternsFound = true;

		while(patternsFound) patternsFound = printPatternsFor(min, max, i++);
	}

	public static boolean printPatternsFor(int min, int max, int starter) {
		int num1 = starter;
		int num2 = starter;
		int temp;
		boolean found = false;
		StringBuffer sb = new StringBuffer();
		sb.append(Integer.toString(starter));
		sb.append(Integer.toString(starter));

		while(Integer.parseInt(sb.toString()) < min) {
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
		if(Integer.parseInt(sb.toString()) <= max) found = true;
		while(Integer.parseInt(sb.toString()) <= max) {
			System.out.println(sb.toString());
			temp = num2;
			num2 = num1 + temp;
			num1 = temp;
			sb.append(Integer.toString(num2));
		}
	
		return found;
	}

	
	public static void main(String[] args) {
		printPatterns(0, 300000);
	}

- JRS February 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;
public class DoubleSum {
	public static void main(String arg[]) {
		List<Integer> list = getNum(1000, 1000000);
		for(Integer a : list) {
			System.out.print(a + " ");
		}
	}
	
	static List<Integer> getNum (int start, int end) {
		List<Integer> list = new ArrayList<Integer>();
		for(int num = start; num <= end; num++) {
			int doubleIndex = getDoubleIndex(num);
			if(doubleIndex != -1) {
				String numStr = new Integer(num).toString();
				int doubleSum = Character.getNumericValue(numStr.charAt(doubleIndex))*2;
				int nextNum = Character.getNumericValue(numStr.charAt(doubleIndex+1));
				//Check for numbers like 5510, 9918
				if(doubleSum > 9 && doubleIndex+2 < numStr.length()) {
					nextNum = Integer.valueOf(numStr.substring(doubleIndex+1, doubleIndex+3));
				}
				
				if(doubleSum == nextNum ) {
					list.add(num);
				}
			}
		}
		return list;
	}
	
	static int getDoubleIndex(int num) {
		char[] numCharArray = new Integer(num).toString().toCharArray();
		int i=0, j=1;
		for(; i < numCharArray.length - 1; i++, j++) {
			if((numCharArray[i] == numCharArray[j]) && j != numCharArray.length-1) {
				return j;
			}
		}
		if(i == numCharArray.length-1) {
			return -1;
		}
		return -1;
	}

}

- Neo February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;
public class DoubleSum {
	public static void main(String arg[]) {
		List<Integer> list = getNum(1000, 1000000);
		for(Integer a : list) {
			System.out.print(a + " ");
		}
	}
	
	static List<Integer> getNum (int start, int end) {
		List<Integer> list = new ArrayList<Integer>();
		for(int num = start; num <= end; num++) {
			int doubleIndex = getDoubleIndex(num);
			if(doubleIndex != -1) {
				String numStr = new Integer(num).toString();
				int doubleSum = Character.getNumericValue(numStr.charAt(doubleIndex))*2;
				int nextNum = Character.getNumericValue(numStr.charAt(doubleIndex+1));
				//Check for numbers like 5510, 9918
				if(doubleSum > 9 && doubleIndex+2 < numStr.length()) {
					nextNum = Integer.valueOf(numStr.substring(doubleIndex+1, doubleIndex+3));
				}
				
				if(doubleSum == nextNum ) {
					list.add(num);
				}
			}
		}
		return list;
	}
	
	static int getDoubleIndex(int num) {
		char[] numCharArray = new Integer(num).toString().toCharArray();
		int i=0, j=1;
		for(; i < numCharArray.length - 1; i++, j++) {
			if((numCharArray[i] == numCharArray[j]) && j != numCharArray.length-1) {
				return j;
			}
		}
		if(i == numCharArray.length-1) {
			return -1;
		}
		return -1;
	}

}

- Neo February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;
//find all numebrs and return an list
public class Solution {
public List<Long> allFibNumbers(long n){
List<Long> list = new ArrayList<Long>();
String s = String.valueOf(n);
long i = 1;
while(String.valueOf(i).length() * 2 < s.length()){
long[] dp = new long[s.length()];
dp[0] = i;
dp[1] = i;
String temp = String.valueOf(dp[0]) + String.valueOf(dp[1]);
for(int j = 2; j < dp.length; j++){
dp[j] = dp[j - 1] + dp[j - 2];
temp += String.valueOf(dp[j]);
if(Long.valueOf(temp).longValue() > n) break;
list.add(Long.valueOf(temp).longValue());
}
i++;
}
return list;
}
}

- Boyang Li March 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python Solution

def rep(start, end):
    #  Simple solution using string concatenation
    numbers = list()
    number = 0
    while number < end:
        # Creates the first number that starts with 2 of the same number,
        # and then the sum of the previous 2
        # example: [112,224,336,...]
        word = (str(start)+str(start)+str(2*start))
        number = int(word)
        if(number < end):
            numbers.append(number)
        i = 1
        number2 = number
        word2 = word
        while number2 < end:
            # Continues to repeat the summation pattern for the
            # number created above (number)
            # example: [1123,2246,3369,...]
            word2 = word2 + str(int(word2[i]) + int(word2[i+1]))
            number2 = int(word2)
            if number2 < end:
                numbers.append(number2)
            i += 1
        start += 1

    return sorted(numbers)

- Rafael March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Fibonachi {

    public static void main(String args[]) {

        //input to print Fibonacci series upto how many numbers
        System.out.print("Enter number from which Fibonacci series to print: ");
        int min = new Scanner(System.in).nextInt();
        System.out.print("Enter number of Fibonacci series to print: ");
        int number = new Scanner(System.in).nextInt();

        System.out.println("\n\nFibonacci series upto " + number +" numbers from "+min+" :");
        //printing Fibonacci series upto number
        System.out.print(min+ " "+min+" ");
        for(int i=min; i<min+number; i++){

            System.out.print(modifiedFibonacci(min, i) +" ");
        }
    }

    // Java program for Fibonacci number using recursion.
    public static int modifiedFibonacci(int min, int number)
    {
        if (number<=min)
            return min+min;
        else
            return modifiedFibonacci(min,number-1)+modifiedFibonacci(min,number-2);
    }
}

- Aziz August 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++

void testq(int lowerbound, int upperbound)
{
	string number = to_string(lowerbound);

	for (int i = 1; stoi(number) <= upperbound; ++i)
	{
		number = to_string(i);
		number.append(to_string(i));
		number.append(to_string(i + i));

		if (stoi(number) > lowerbound)
			cout << number << endl;
	}

	return;

}

- Anonymous October 24, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Python. Use index exception to catch numbers with less than three digits. A bit slow but less code.

def matched_numbers(first_number, last_number):
    result = []
    #check for valid interval
    if not first_number <= last_number:
        return []
    #check each number in the
    for shift in range(last_number - first_number + 1):
        if pattern_match(first_number + shift):
            result.append(first_number + shift)
    return result


# Return True if pattern matched
def pattern_match(n):
    try:
        s = str(n)
        if (int(s[0]) == int(s[1])) and int(s[0]) + int(s[1]) == int(s[2]):
            return True
        else:
            return False

    # if the number is too small, handle the exception here
    except IndexError:
        return False

- Anonymous February 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This python solution is wrong as it is only correct when the 2nd digit is equal to the 1st and 3rd is the sum of first two.
What if the number is 111122. Where 1st number is 11 which is equal to the second number(11) and the third number (22) is the their sum.

- jainAnks February 26, 2015 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More