ThoughtWorks Interview Question for Applications Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
11
of 13 vote

We can use class.. No recursion, No loops..

class Print
{
    
public:
    Print()
    {
        static int count = 0;
        cout<<"h"<<++count<<endl;
    }
};

int main()
{    
    Print print[1000];
    getchar();
    return 0;
}

- Hitesh Vaghani September 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good one

- kd September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you..

- Hitesh Vaghani September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice :)

- Abhey September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just Awesome B-)

- Mukesh September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course, internally the compiler will create some sort of loop to do this. But what other solutions are we to offer? Of course we could copy and paste the print command 1000 times, but that's an awful solution.

I guess we could do something like print2() { print(); print(); } print4() {print2(); print2(); }... etc. to reduce the code size, but it's still not easy to adjust when you want to change 1000 to some other value.

- eugene.yarovoi September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no loops my friend . i an just calling constructor 1000 times..
Thanks Mukesh and Abhey.

- Hitesh Vaghani September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Of course, internally the compiler will create some sort of loop to do this."

Notice the keyword "internally". How do you think the C++ compiler achieves the initialization of the objects in the array? In the compiled code, it generates a loop to do it.

- eugene.yarovoi September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome...

- coderBaba September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any one has idea how we can do this in JAVA ?
Can we call such block multiple times by just mentioning the count.

- Himanshu September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Himanshu: I'm not aware of any way to just run a static block of code a specified number of times. Some languages have features like that, but not Java, as far as I remember.

However, for this specific problem, which is a little more limited in scope, you can use the technique I outlined in one of my other comments.

- eugene.yarovoi September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@coderBaba Thank you..

- Hitesh Vaghani September 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class Tester004 {

    public static void main(String[] args) {
        int limit=5;
        char c='a';
        go(0,limit,c);
    }

    public static void go(int i,int limit, char c) {
        if (i < limit) {
            System.out.println(c);
            i++;
            go2(i,limit,c);
        }
    }

    public static void go2(int i,int limit, char c) {
        if (i < limit) {
        System.out.println(c);
        i++;
        go(i,limit,c);
        }
    }
}

- Amit Petkar April 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

its just like calling a function 1000 times where u can print the character..this method takes a lot of time to compile the code..pls suggest a better one

- Anonymous July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

In embedded scenario: Configure a timer interrupt, in the ISR keep writing the character until a counter variable reaches 1000.

In Desktop App: Use Timer class from Java Utils or equivalent API routines which give the feature of periodic execution of a function.

- kd September 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I could argue that this too is a loop.

There isn't a clear line between what is a loop and what isn't.

- Anonymous September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But otherwise, good answer.

- Anonymous September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

if Python, print 'a' * 1000

if C or C++,

const int BUFFER_SIZE = 1000;
    char * a = (char *)malloc(BUFFER_SIZE + 1);
    memset(a,'a', BUFFER_SIZE);
    a[BUFFER_SIZE] = 0;
    printf("%s",a);
    free(a);

- yhjang September 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course memset uses a loop internally

- Anonymous September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

C# (via Lambda Linq)
char[] word = new char[10];
word = word.Select(x => x = 'a').ToArray();
Console.WriteLine(word);
Console.ReadLine();

- Anonymous September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cooooool one!!!

- Mukesh Kumar Verma September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Label and goto statement may be used for the solution.

count=0;
label:
// print here
if(++count<1000) goto label;

- mcg September 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This too is a loop.

- kd September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, that depends on your definition of "loop".

- Anonymous September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah right. In idealistic terms, loop can be a way through which same code can be executed repeatedly.

If considered specifically in terms of programming language feature, for, while, goto, etc. directly become looping constructs.

Either way its all relative. No hard rules. :)

- Krishna Devale September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if C++, you can use templates.

Was there a specific language required?

- Anonymous September 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In C++ you could also make a constructor for a class do it, and then make an array of size 1000 of that class.

- Anonymous September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The problem with templates is that if I recall correctly, they're expanded at compile time. Meaning the compiled version of your code will have 1000 print calls, possibly bloating the code size (for 1000 it's not so bad, but what if you need 1000000?)

- eugene.yarovoi September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Exactly. So this is the right solution. No loops, recursion whatsoever!

If you do it at runtime, you will end up having some kind of a loop!

So your comment about 10^blah is irrelevant.

- Anonymous September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

eh...

The right solution is to ask the interviewer to define the constraints on the problem better, and define the desired tradeoffs better.

Absent that, we can only guess what might have been wanted here. Most of the answers here are right, from a certain perspective...

- eugene.yarovoi September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course. There is no "right" answer. Especially given the absence of the interviewer.

- Anonymous September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

but how in java because that is possible in c++ only

- altaf September 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A solution of a similar flavor in Java would be to create an array of characters, char[1000], call Arrays.fill (yourCharArray, 'a'), construct a new String from the char array, and then print the string.

Of course, Arrays.fill, like most other solutions here, uses a loop internally. I think that's probably going to have to be OK, because what are we to offer that doesn't?

- eugene.yarovoi September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using setjmp and longjmp

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

int main()
{
  jmp_buf env;
  
  int val;
  char a = 'c';

  // first time returns 0
  val = setjmp(env);

  printf ("%c",a);

  if (val < 999) 
	  longjmp(env, ++val);

  return 0;

}

- abhi September 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not familiar with this particular library. Can you explain what this does?

- eugene.yarovoi September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is similar function as goto, but more powerful.

- Ares.Luo.T September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Any one has idea how we can do this in JAVA ?

- Himanshu September 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Have you read the other responses? I've already commented on this.

- eugene.yarovoi September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Reproducing the comment here for your convenience:

A solution of a similar flavor in Java would be to create an array of characters, char[1000], call Arrays.fill (yourCharArray, 'a'), construct a new String from the char array, and then print the string.

Of course, Arrays.fill, like most other solutions here, uses a loop internally. I think that's probably going to have to be OK, because what are we to offer that doesn't?

- eugene.yarovoi September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check my solution below.

- JaimeA September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@JaimeA: sorry, but your solution doesn't satisfy the criteria of this problem. See my comment to it.

- eugene.yarovoi September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my solution with Java

public class A extends B{
  
    static int m = 0;
    public A()
    {
        
    }
    public static void main(String args[])
    {
        B b = new B();
    }
}
public class B {
    A c;
    
    public B()
    {
        A.m++;
        System.out.println("e");
        if(A.m < 1000)
        {
            c = new A();
            
        }
    }
    
}

- JaimeA September 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a very bizarre form of recursion, which the problem statement forbids.

- eugene.yarovoi September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Super solution

- A April 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sir what was the need to use inheritence?,we could simply use two different classes

- pank.bh92 May 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution.. Thank you!!

- MayaA January 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here in Ruby I'll def a method that pass in the character to be printed 1000 times:

def print1000 (a)
puts a*1000
end

#problem?

- sonnyhe2002 September 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is a very simple way to display a character 1000 time in Ruby. PSB
suppose the character to be print is 's'.

arr = Array.new(1000,'s')
puts arr

Thats it. it will print 's' 1000 times

- Sanoop November 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use labels for printing the numbers from 1 to 1000.

void main()
{
          int i = 1;
          label1:
                 cout<<i++;
                if( i <= 1000 )
                   goto label1;
}

- Veeru September 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Forgot to print the character. Below is the modified code.

void main()
{
          int i = 1;
          char ch='v';
          label1:
                 cout<<ch;
                if( i++ <= 1000 )
                   goto label1;
}

- Veeru September 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a loop

- Pepe September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be we can write two methods, calling each other.

public void func1(String inputStr, int count) {
		
		if ( count != 1000 ) {
			
			System.out.println(inputStr);
			count++;
			func2(inputStr, count);
		}	
	}

	public void func2(String inputStr, int count) {
		
		if ( count != 1000 ) {
			
			System.out.println(inputStr);
			count++;
			func1(inputStr, count);
		}	
	}

The initial value of count is 0.

- belligerentCoder September 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

initial call can be either
func1("hello", 0)

or

func2("hello", 0)

- belligerentCoder September 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How is this not recursion? Methods calling themselves, even indirectly, is still recursion.

- eugene.yarovoi September 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class Print
{
static int count=1;
public static void func1() {

if ( count != 1001 ) {

System.out.println("value of count"+count);
count++;
func2();
}
}

public static void func2() {

if ( count != 1001 ) {

System.out.println("value of count"+count);
count++;
func1();
}
}

public static void main(String args[])
{
func1();
}
}

- imran September 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in javascript

var y=Math.pow(2, 1000);
x=y.toString(2);
var ans=x.replace(/[0]/g,'a').replace(/[1]/g,'');

document.write(ans);

}

- Anonymous September 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

StringBuffer a = new StringBuffer(1000);
a.setLength(1000);
System.out.println(a.toString().replace('\u0000', 'A'));

- AM April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
{
static void Main(string[] args)
{
new MyClass();
}
}

public class MyClass
{
private static int _count = 0;
public MyClass()
{
if (++_count == 1000) return;
Console.Write("a");
new MyClass();
}
}

- Anonymous April 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

idk if i'm correct

int i=1;
char name ='a'
Label1: printf(name);
i++;
if(i<1001)
exit();
else goto label1;

goto

- Anonymous August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] word = new char[10];
word = word.Select(x => x = 'a').ToArray();
string res = string.Join("", word.ToArray());

- Nava August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

gcc provieds a way to initalize an array at time of defining it. We may use this solution (look at gcc -s output for more details):

#include<stdio.h>
char a[1001]={[0 ... 999] = 'a'};
main() {
        printf("%s\n", a);
        return 0;
}

- Sumit September 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"gcc -S" is the right option to generate the aseembly code. Sorry for typo in the last meesage.

- Sumit September 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#PRINT A CHARACTER 1000 TIMES 
x = raw_input("Enter a character " )
x = x * 10000
print(x)

- rajan raj September 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php
function printval($i, $range){
if($i <= $range){
echo $i."<br>";
printval($i+1, $range);
}else{
exit;
}
}

printval(0, 10);
?>

- Anshul Jain December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In ruby simply we can do it like, it works but i doubt its right way to approach

puts "enter ur charector"
val= gets.chomp
puts val*1000

- rahul January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I wrote two classes A and B . A calls B and B calls A untill count becomes 1000. Its Ruby code.


class A
def initialize(val,count)
@val= val
@count= count
if @count <= 1000 then
puts "#{@val} in class A count= #{@count}"
@count = @count+1
B.new(@val,@count)
end
end
end

class B
def initialize(val,count)
@val= val
@count= count
if @count <= 1000 then
puts "#{@val} in class B count= #{@count}"
@count = @count+1
A.new(@val,@count)
end
end
end

puts "Enter your charector"
val= gets.chomp
count= 0
A.new(val,count)

- Rahul sambari January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{-- what about below code?
public void printCharNTimes(char c, int n){
String [] a = new String [n];
List<String> l = Arrays.asList(a);
Collections.fill(l, String.valueOf(c));
System.out.println(l);
}

-- it prints the character with extra ',' in between....}

- newHere February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;
public class test{
public static void main(String args[])
{
char[] pr = new char[10];
Arrays.fill(pr,0,9,'a');
System.out.println(Arrays.toString(pr).replaceAll("(\\[|\\]|,| )", ""));
}
}

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

class Repeat{
 	public static void main(String[] args) {
 		String magic = new String(new char[10]).replace("\0", "a\n");
 		System.out.println(magic);
 	}
 	
 }

- Anonymous July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Printx{
  public static void main(String[] args){
    char[] my_char = new char[1000];
    java.util.Arrays.fill(my_char, 'c');
    System.out.println(String.valueOf(my_char));
  }
}

- sovanlandy October 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We could employ timer callbacks for the same. Found some CPP code prints "tick" continuously until interrupted. I modified the same to print char "a" a thousand time. Please check if this will do?

#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace boost::asio;
using namespace std;

class Deadline 
{
public:
    Deadline(deadline_timer &timer) : t(timer) {
        wait();
        count = 0;
    }

    void timeout(const boost::system::error_code &e) {
        if (e)
            return;
        if (count < 1000)
        {
            cout << "a" << endl;
        }
	else
	{
	    return;
	}
        count++;
        wait();
    }

    void cancel() {
        t.cancel();
    }


private:
    void wait() {
        t.expires_from_now(boost::posix_time::seconds(1)); //repeat rate here
        t.async_wait(boost::bind(&Deadline::timeout, this, boost::asio::placeholders::error));
    }

    deadline_timer &t;
    int count;
};


class CancelDeadline {
public:
    CancelDeadline(Deadline &d) :dl(d) { }
    void operator()() {
        string cancel;
        cin >> cancel;
        dl.cancel();
        return;
    }
private:
    Deadline &dl;
};



int main()
{
    io_service io;
    deadline_timer t(io);
    Deadline d(t);
    CancelDeadline cd(d);
    boost::thread thr1(cd);
    io.run();
    return 0;
}

- Ajith November 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package learn;

import java.lang.reflect.Array;

/**
 * Created by comp17 on 12-11-2014.
 */
public class PrintChars {
    private int limit;
    private char chr;

    public PrintChars(int limit, char chr){
        this.limit = limit;
        this.chr = chr;
    }

    private void printCharsNTimes(){
        String sb = new String(new char[this.limit]);
        System.out.println(sb.replaceAll("", String.valueOf(this.chr)));
    }
    public static void main(String [] args){
        PrintChars printChars = new PrintChars(10, 'M');
        printChars.printCharsNTimes();

        }

}

- Anonymous November 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using C#:

static void Main(string[] args)
{
int i = 1;

Found:
if(i<=1000)
{
Console.WriteLine("a");
i++;
goto Found;
}
Console.ReadLine();
}

- Nirvana April 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Java

import java.io.*;

public class A {
static int cnt=0;
public A()
{
B n=new B();
}

public static void main(String [] args)
{
A d=new A();

}
public class B{
public B(){
A c;
System.out.println("p");
cnt++;
if(cnt<1000)
c=new A();
}
}
}

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

public  static  void  main (String[] args ){

String[] arr =new String[100];

Arrays.fill(arr, "Indu");

List list = new ArrayList();

list = Arrays.asList(arr);

System.out.println(list);

}

- Indu August 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main (String[] args ){
String[] arr =new String[100];
Arrays.fill(arr, "Indu");
List list = new ArrayList();
list = Arrays.asList(arr);
System.out.println(list);
}

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

public  static  void  main (String[] args ){
        String[] arr =new String[100];
        Arrays.fill(arr, "Indu");
        List list = new ArrayList();
        list = Arrays.asList(arr);
        System.out.println(list);
    }

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

public static void main (String[] args ){
String[] arr =new String[100];
Arrays.fill(arr, "Indu");
List list = new ArrayList();
list = Arrays.asList(arr);
System.out.println(list);
}

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

public  static  void  main (String[] args ){
        String[] arr =new String[100];
        Arrays.fill(arr, "Indu");
        List list = new ArrayList();
        list = Arrays.asList(arr);
        System.out.println(list);
    }

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

public  static  void  main (String[] args ){

String[] arr =new String[100];

Arrays.fill(arr, "Indu");

List list = new ArrayList();

list = Arrays.asList(arr);

System.out.println(list);

}

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

public  static  void  main (String[] args ){
        String[] arr =new String[100];
        Arrays.fill(arr, "Indu");
        List list = new ArrayList();
        list = Arrays.asList(arr);
        System.out.println(list);
    }

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

perl provides a very simple solution to this. as it has 'x' which multiplies(read prints) the number x number of times

# it will print 1 or any variable 1000 times, and no loop or recursion is required
{
$var=1;
print $var x 1000;
}

- Gaurav Khurana October 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Sample {

static int count = 0;
{
if(count < 1000)
{
count++;
System.out.print(count);
new Sample();
}
}

public static void main(String[] args) {

Sample sample = new Sample();

}

}

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

public class Sample {
static int count = 0;
{
if(count < 1000)
{
count++;
System.out.print("a");
new Sample();
}
}

public static void main(String[] args) {

Sample sample = new Sample();

}

}

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

public class Sample
{
static int count = 0;
{
if(count < 1000)
{
count++;
System.out.print(count);
new Sample();
}
}
public static void main(String[] args) {
Sample sample = new Sample();
}
}

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

{
public class Sample {

static int count = 0;
{
if(count < 1000)
{
count++;
System.out.print(count);
new Sample();
}
}

public static void main(String[] args) {

Sample sample = new Sample();

}

}
}

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

public class Sample {
static int count = 0;
{
if(count < 1000)
{
count++;
System.out.print(count);
new Sample();
}
}
public static void main(String[] args) {
Sample sample = new Sample();
}
}

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

public class Sample {
static int count = 0;
{
if(count < 1000)
{
count++;
System.out.print(count);
new Sample();
}
}
public static void main(String[] args) {
Sample sample = new Sample();
}
}

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

def rep2(block : =>Unit) = {block;block}
def rep10(block : =>Unit) = {rep2({rep2(rep2(block)); block})}
rep10(rep10(rep10(println("a"))))

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

C# .

var a1000Times = new String('a', 1000);
            Console.Write(a1000Times);

- nkdram January 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be done using String constructor which takes a character and number of times it needs to copied.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Employee
{
    public class Contact
    {

        public string Name { get; private set; }
        public string PhoneNumber { get; private set; }
        public string Address { get; private set; }
        public string _All
        {
            get
            {
                return string.Format("{0}:{1}:{2}",
                    this.Name,
                    this.PhoneNumber,
                    this.Address).ToLower();
            }
        }

        public Contact(string name, string phoneNumber, string address)
        {
            this.Name = name;
            this.PhoneNumber = phoneNumber;
            this.Address = address;
        }

       
    }

    public class PhoneBook
    {
        private readonly IEnumerable<Contact> _contact;

        public PhoneBook(IEnumerable<Contact> contact)
        {
            this._contact = contact;
        }
        public IEnumerable<Contact> lookForName(string name)
        {
            var lookup = _contact.ToLookup(nd => nd.Name);
            return lookup[name];
        }
        public IEnumerable<Contact> lookForPhoneNumber(string phoneNumber)
        {
            var lookup = _contact.ToLookup(nd => nd.PhoneNumber);
            return lookup[phoneNumber];
        }

        public IEnumerable<Contact> lookForAlphabet(char letter)
        {
            var lookup = _contact.ToLookup(p => Convert.ToChar(p.Name.Substring(0, 1)),
                  p => p);
            return lookup[letter];
        }

        public IEnumerable<Contact> lookForAll(string anyVal)
        {
            var lookup = _contact.Where(p => p._All.Contains(anyVal.ToLower()));
            return lookup;
        }
    }

    class CustomComparer : IEqualityComparer<Contact>
    { 
    
    }
}

- nkdramkumar January 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var a1000Times = new String('a', 1000);
Console.Write(a1000Times);

- nkdramkumar January 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Print{
    public static int count=0;
    Print(){
        count++;
        System.out.print("d ");
        if(count<10){
            new Print();
        }
    }
    public static void main( String[] args ){
        Print p= new Print();
    }
}

- Anonymous June 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int main()
{
static int i=1;
A:
if(i<100)
{
printf("H");
i++;
goto A;
}

getch();
return 0;
}

- abhishek September 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int main()
{
static int i=1;
A:
if(i<100)
{
printf("H");
i++;
goto A;
}

getch();
return 0;
}

- abhishek September 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Child extends Parent{
  
    static int counter = 0;
    public Child()
    {
        super();
    }
    public static void main(String args[])
    {
    	new Parent();
    }
}
 class Parent {
	 Child c;
    
    public Parent()
    {
    	Child.counter++;
        System.out.println("e");
        if(Child.counter < 1000)
        {
            c = new Child();
            
        }
    }

}

- Anonymous October 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Child extends Parent{

static int counter = 0;
public Child()
{
super();
}
public static void main(String args[])
{
new Parent();
}
}
class Parent {
Child c;

public Parent()
{
Child.counter++;
System.out.println("e");
if(Child.counter < 1000)
{
c = new Child();

}
}

}

- Ashish October 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use timers:
//Javascript code

var counter = 0;
setInterval(function(){if(++counter < 1001)console.log('a');},10);

- Himanshu October 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes

- Anonymous March 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void UnitTest1()
        {
            var numberContext = new NumberContext(100);

            numberContext.OnNumberChange += OnNumberChange(numberContext);
            numberContext.CurrentNumber = 1;
        }

        OnNumberChangeHandler OnNumberChange(NumberContext numberContext)
        {
            return (o, args) =>
            {
                if (args.Counter > numberContext.LastNumber)
                    return;

                Console.WriteLine(numberContext.CurrentNumber);

                args.Counter += 1;
                numberContext.CurrentNumber = args.Counter;
            };
        }
    }

    public delegate void OnNumberChangeHandler(object source, OnNumberChangeEventArgs e);
    public class NumberContext
    {
        public NumberContext(int lastNumber)
        {
            LastNumber = lastNumber;
        }

        public event OnNumberChangeHandler OnNumberChange;
        private int currentNumber;
        public int CurrentNumber
        {
            get { return currentNumber; }
            set {
                currentNumber = value;
                OnNumberChange(this, new OnNumberChangeEventArgs(value));
            }
        }

        public int LastNumber { get; set; }

        public class OnNumberChangeEventArgs : EventArgs
        {
            public OnNumberChangeEventArgs(int counter)
            {
                Counter = counter;
            }

            public int Counter { get; set; }
        }
    }

- LucidCoder December 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void UnitTest1()
        {
            var numberContext = new NumberContext(100);

            numberContext.OnNumberChange += OnNumberChange(numberContext);
            numberContext.CurrentNumber = 1;
        }

        OnNumberChangeHandler OnNumberChange(NumberContext numberContext)
        {
            return (o, args) =>
            {
                if (args.Counter > numberContext.LastNumber)
                    return;

                Console.WriteLine(numberContext.CurrentNumber);

                args.Counter += 1;
                numberContext.CurrentNumber = args.Counter;
            };
        }
    }

    public delegate void OnNumberChangeHandler(object source, OnNumberChangeEventArgs e);
    public class NumberContext
    {
        public NumberContext(int lastNumber)
        {
            LastNumber = lastNumber;
        }

        public event OnNumberChangeHandler OnNumberChange;
        private int currentNumber;
        public int CurrentNumber
        {
            get { return currentNumber; }
            set {
                currentNumber = value;
                OnNumberChange(this, new OnNumberChangeEventArgs(value));
            }
        }

        public int LastNumber { get; set; }

        public class OnNumberChangeEventArgs : EventArgs
        {
            public OnNumberChangeEventArgs(int counter)
            {
                Counter = counter;
            }

            public int Counter { get; set; }
        }
    }

- LucidCoder December 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Javascript

function printCharacter(character,times) {
  console.log(character.repeat(times));
}

printCharacter('H',200);

- vvkpd March 27, 2019 | Flag Reply


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